【问题标题】:Object type '(null)' not persisted in Realm对象类型“(null)”未在领域中保留
【发布时间】:2015-07-31 10:17:34
【问题描述】:

我有这个领域对象,我正试图将其保存到磁盘但不断收到此错误:

对象类型“(null)”未在 Realm 中持久化。

这是对象的代码:

channel_modal.h

@interface channel_modal : RLMObject
@property (strong, nonatomic) NSString* channelNumber;
@property (strong, nonatomic) NSString* displayTitle;
@property (strong, nonatomic) NSString* ID;
@property (strong, nonatomic) NSString* ChannelUri;
@property (strong, nonatomic) NSString* mediaType;
@property (strong, nonatomic) NSString* isHD;
@property (strong, nonatomic) NSString* imageUri;
@property (strong, nonatomic) source_modal* source;
@end

// This protocol enables typed collections. i.e.:
// RLMArray<channel_modal>
RLM_ARRAY_TYPE(channel_modal)

channel_modal.m

#import "channel_modal.h"

@implementation channel_modal
{
    NSDictionary* dictionary;
}

+ (NSArray *)ignoredProperties
{
    return @[@"dictionary"];
}

- (bool)writeToRealm:(RLMRealm*)realm {
    @try {
        [realm addObject:self];
        return YES;
    }
    @catch (NSException *exception) {
        NSLog(@"Realm exception: %@", [exception reason]);
        return NO;
    }
    @finally {
        return NO;
    }
}

- (void)setupObjectForDatabase {
    self.channelNumber = [self getChannelNumber];
    self.displayTitle = [self getChannelTitle];
    self.ID = [self getChannelID];
    self.ChannelUri = [self getChannelUri];
    self.mediaType = [self getChannelMediaType];
    self.isHD = [self getChannelHD];
    self.imageUri = [self getChannelImage:0];
    self.source = [self getChannelSource];
//    self.channelSchedule = [self getChannelSchedule];
}


#pragma mark - Get Methods
-(NSString*)getChannelNumber {
    if ([dictionary objectForKey:@"channel_number"] == nil)
        return @"";
    return (NSString*)[dictionary objectForKey:@"channel_number"];
}

-(NSString*)getChannelTitle {
    if (([dictionary objectForKey:@"channel"] == nil) || ([[dictionary objectForKey:@"channel"] objectForKey:@"title"] == nil))
        return @"";
    return (NSString*)[[dictionary objectForKey:@"channel"] objectForKey:@"title"];
}

-(NSString*)getChannelID {
    if (([dictionary objectForKey:@"channel"] == nil) || ([[dictionary objectForKey:@"channel"] objectForKey:@"id"] == nil))
        return @"";
    return (NSString*)[[dictionary objectForKey:@"channel"] objectForKey:@"id"];
}

-(NSString*)getChannelUri {
    if (([dictionary objectForKey:@"channel"] == nil) || ([[dictionary objectForKey:@"channel"] objectForKey:@"uri"] == nil))
        return @"";
    return (NSString*)[[dictionary objectForKey:@"channel"] objectForKey:@"uri"];
}

-(NSString*)getChannelMediaType {
    if (([dictionary objectForKey:@"channel"] == nil) || ([[dictionary objectForKey:@"channel"] objectForKey:@"media_type"] == nil))
        return @"";
    return (NSString*)[[dictionary objectForKey:@"channel"] objectForKey:@"media_type"];
}

-(NSString*)getChannelHD {
    if (([dictionary objectForKey:@"channel"] == nil) || ([[dictionary objectForKey:@"channel"] objectForKey:@"high_definition"] == nil))
        return @"";
    return (NSString*)[[dictionary objectForKey:@"channel"] objectForKey:@"high_definition"];
}

-(NSString*)getChannelImage:(int)imageIndex {
    if ([[dictionary objectForKey:@"channel"] objectForKey:@"images"] == nil)
        return @"";

    NSArray* imagesArray = [[dictionary objectForKey:@"channel"] objectForKey:@"images"];

    if ([imagesArray count] < 1)
        return @"";

    if ((imageIndex > [imagesArray count]) || ([imagesArray objectAtIndex:imageIndex] == nil))
        return @"";

    NSDictionary* imageObjectAtIndex = [imagesArray objectAtIndex:imageIndex];

    if ([imageObjectAtIndex objectForKey:@"uri"] == nil)
        return @"";

    return (NSString*)[imageObjectAtIndex objectForKey:@"uri"];
}

- (source_modal*)getChannelSource {
    if (([dictionary objectForKey:@"channel"] == nil) || ([[dictionary objectForKey:@"channel"] objectForKey:@"source"] == nil))
        return [[source_modal alloc] init];

    NSDictionary* sourceDict = [[dictionary objectForKey:@"channel"] objectForKey:@"source"];
    source_modal* source = [[source_modal alloc] initWithNSDictionary:sourceDict];

    return source;
}

这是我用来写入磁盘的代码:

RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[channel writeToRealm:realm];
[realm commitWriteTransaction];

我在运行时检查了 source_modal 和 channel_modal 对象,在它们中看不到任何空值。

这是发生异常的回溯:

2015-08-01 18:36:06.881 babblebox[1231:13309] *** Terminating app due to uncaught exception 'RLMException', reason: 'Object type '(null)' not persisted in Realm'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ace3c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a97cbb7 objc_exception_throw + 45
    2   babblebox                           0x0000000108c2070b -[RLMSchema objectForKeyedSubscript:] + 219
    3   babblebox                           0x0000000108ba260b RLMAddObjectToRealm + 395
    4   babblebox                           0x0000000108bf9ed8 -[RLMRealm addObject:] + 40
    5   babblebox                           0x0000000108b34d3e -[channel_modal writeToRealm:] + 62
    6   babblebox                           0x0000000108b4d8a4 __39-[GuideViewController downloadChannels]_block_invoke + 788
    7   libdispatch.dylib                   0x000000010b9f0186 _dispatch_call_block_and_release + 12
    8   libdispatch.dylib                   0x000000010ba0f614 _dispatch_client_callout + 8
    9   libdispatch.dylib                   0x000000010b9f9552 _dispatch_root_queue_drain + 1768
    10  libdispatch.dylib                   0x000000010b9fab17 _dispatch_worker_thread3 + 111
    11  libsystem_pthread.dylib             0x000000010bd7c637 _pthread_wqthread + 729
    12  libsystem_pthread.dylib             0x000000010bd7a40d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

  • 我已经查看了 source_modal 或 channel_modal 的任何属性中的任何 nil 值。我想不出还有什么可以保存的。
  • 没有defaultREalm?
  • 在全新安装应用程序后会发生这种情况吗?如果不是,则可能是迁移问题。
  • 我虽然这样。我在应用程序委托中放置了一些代码,以实际完全删除数据库文件。
  • 您能否分享异常发生位置的回溯?

标签: objective-c caching realm


【解决方案1】:

您的source_uri 类是否可能不是RLMObject 的子类?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多