对象类型'(null)'不坚持在境界

时间:2015-07-31 10:17:35

标签: objective-c caching realm

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

  

对象类型'(null)'不留在境界。

这是对象的代码:

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) 

1 个答案:

答案 0 :(得分:0)

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