Core Data属性返回0的问题

时间:2011-03-31 00:29:12

标签: iphone core-data nsstring nsdate nsmanagedobject

  

可能重复:
  Core Data not saving NSString.

嘿所有!

目前在我的最新应用程序中遇到了Core Data的一个奇怪问题。基本上我正在完成的是解析JSON并将每个对象添加到相应属性下的Core Data中。从我已经设置的NSLog中可以看出,这个工作正常。但是,当在UITableView中显示其中一个值时,它总是因某种未知原因返回0。

这是我用来将对象存储到Core Data实体中的函数。

-(void)syncNotes {

UIApplication *app = [UIApplication alloc];
app.networkActivityIndicatorVisible = YES;

NSDictionary *params = [NSDictionary dictionaryWithObject:authToken forKey:@"api_key"]; 

[[LRResty client] get:@"http://notacio.us/api/note" parameters:params withBlock:^(LRRestyResponse *response){

    if(response.status == 200) {
        NSLog(@"Successful Connection \n%@", [response asString]);

        // Create SBJSON object to parse JSON
        SBJSON *parser = [[SBJSON alloc] init];

        // parse the JSON string into an object - assuming [response asString] is a NSString of JSON data
        NSDictionary *object = [parser objectWithString:[response asString] error:nil];

        NSFetchRequest *noteFetch;
        NSManagedObject *newNote;

        appDelegate =(NotaciousAppDelegate *) [[UIApplication sharedApplication] delegate];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
        NSArray *fetchedNotes;

        NSArray *notes = [object valueForKey:@"result"];
        for (NSDictionary *singleNote in notes){

            noteFetch=[[NSFetchRequest alloc] init];
            [noteFetch setEntity:entity];
            NSPredicate *pred=[NSPredicate predicateWithFormat:@"ID==%@",[singleNote objectForKey:@"note id"]];
            [noteFetch setPredicate:pred];

            NSError *fetchError=nil;
            fetchedNotes=[appDelegate.managedObjectContext executeFetchRequest:noteFetch error:&fetchError];

            if (fetchError!=nil) {
                NSLog(@"syncNotes fetchError=%@,details=%@",fetchError,fetchError.userInfo);
            }
            if ([fetchedNotes count]==0) {


                NSString *notelocked = [singleNote objectForKey:@"note locked"];

                NSString *notecreated = [singleNote objectForKey:@"note created"];
                NSString *noteupdated = [singleNote objectForKey:@"note updated"];
                NSString *notetitle = [singleNote objectForKey:@"note title"];
                NSString *notesummary = [singleNote objectForKey:@"note summary"];
                NSString *noteid = [singleNote objectForKey:@"note id"];
                NSString *notecontent = [singleNote objectForKey:@"note content"];

                NSLog(@"Note Title: %@",notetitle);
                NSLog(@"Note Summary: %@",notesummary);
                NSLog(@"Note ID: %@",noteid);
                NSLog(@"Note Content: %@",notecontent);
                NSLog(@"Note Created: %@",notecreated);
                NSLog(@"Note Updated: %@",noteupdated);
                NSLog(@"Note Locked: %@",notelocked);


                newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
                [newNote setValue:notecontent forKey:@"content"];
                [newNote setValue:notesummary forKey:@"summary"];
                [newNote setValue:notetitle forKey:@"title"];
                [newNote setValue:noteid forKey:@"ID"];
                [newNote setValue:notecreated forKey:@"created"];
                [newNote setValue:noteupdated forKey:@"updated"];
                [newNote setValue:notelocked forKey:@"locked"];

                NSError *error = nil;
                if (![appDelegate.managedObjectContext save:&error]) {
                    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                    abort();
                }


            }

        }

        [noteFetch release];
        app.networkActivityIndicatorVisible = NO;

    }

    if (response.status == 404) {
        NSLog(@"FAIL\n%@", [response asString]);
        app.networkActivityIndicatorVisible = NO;
    }

}];

}

因此,NSLog从我输出的字符串代表从JSON解析的对象的字符串,并将存储如下:

2011-03-31 10:52:23.334 Notacious[755:707] Note Title: Business Draft
2011-03-31 10:52:23.335 Notacious[755:707] Note Summary: Make sure you get one to Pez by end of w
2011-03-31 10:52:23.336 Notacious[755:707] Note ID: 676
2011-03-31 10:52:23.336 Notacious[755:707] Note Content: Business Draft
Conclusion is about 150 words, the rest is for the body of the report.
2011-03-31 10:52:23.337 Notacious[755:707] Note Created: Thu Feb 24 17:02:13 -0800 2011
2011-03-31 10:52:23.339 Notacious[755:707] Note Updated: Thu Feb 24 17:10:43 -0800 2011
2011-03-31 10:52:23.339 Notacious[755:707] Note Locked: 0

正如您所看到的,为所有内容设置了值。但是,我的问题在这里有点奇怪。在设置UITableViewCell并尝试获取更新的值时,它返回0.我正在调用这样的值:

NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

NSLog(@"%@",[[managedObject valueForKey:@"title"] description]);
NSLog(@"%@",[[managedObject valueForKey:@"updated"] description]);

NSString *updated = [[managedObject valueForKey:@"updated"] description];
NSString *parsedDate = [NSDate getNoteTime:updated];

NSLog(@"Before Format: %@",updated);
NSLog(@"After Format: %@",parsedDate);

反过来记录以下内容:

2011-03-31 10:56:22.544 Notacious[755:707] Business Draft
2011-03-31 10:56:22.545 Notacious[755:707] 0
2011-03-31 10:56:22.548 Notacious[755:707] time0
2011-03-31 10:56:22.549 Notacious[755:707] note:0, current:1301531182
2011-03-31 10:56:22.550 Notacious[755:707] Before Format: 0
2011-03-31 10:56:22.550 Notacious[755:707] After Format: ********

正如您所看到的,更新的字符串返回0.任何人都可以了解为什么会发生这种情况?

修改

我尝试用创建的valueForKey来做,而你永远不会猜到发生了什么。它工作得非常好。因此,我唯一能想到的是,当我们将值保存到Core Data属性时,它无法正常保存。这很奇怪,因为创建和更新的属性都是NSStrings。实际上,Core Data实体中的所有内容都是NSStrings。

1 个答案:

答案 0 :(得分:1)

最可能的解释是,字典返回的对象类与实体所期望的对象之间存在不匹配。例如。 noteUpdated应该是一个日期,但代码中没有任何内容可以强制执行。如果您尝试使用无法解析的字符串值设置日期,则日期可能会报告值为零。您应该检查字典返回的类,以查看它们是否适合分配给它们的属性。

此外,这种描述的使用是不必要的:

NSLog(@"%@",[[managedObject valueForKey:@"title"] description]);

...因为NSLog无论如何都会发送描述消息。你在这里有描述返回的字符串的描述。这可能会也可能不会准确反映对象的价值。只需使用:

NSLog(@"%@",[managedObject valueForKey:@"title"]);

进行故障排除时,我建议您直接打印托管对象:

NSLog(@"%@",manageObject);

...这会让你看到它的所有内容都处于原始状态。