Iphone 核心数据属性返回0时出现问题
可能重复:Iphone 核心数据属性返回0时出现问题,iphone,core-data,nsstring,nsdate,nsmanagedobject,Iphone,Core Data,Nsstring,Nsdate,Nsmanagedobject,可能重复: 大家好 当前在我的最新应用程序中遇到核心数据的奇怪问题。基本上,我要完成的是解析JSON并将每个对象添加到相应属性下的核心数据中。从我设置的NSLog中可以看出,这很好。但是,在UITableView中显示其中一个值时,由于未知原因,它总是返回0 下面是我用来将对象存储到核心数据实体中的函数 -(void)syncNotes { UIApplication *app = [UIApplication alloc]; app.networkActivityIndicatorVisi
大家好 当前在我的最新应用程序中遇到核心数据的奇怪问题。基本上,我要完成的是解析JSON并将每个对象添加到相应属性下的核心数据中。从我设置的NSLog中可以看出,这很好。但是,在UITableView中显示其中一个值时,由于未知原因,它总是返回0 下面是我用来将对象存储到核心数据实体中的函数
-(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来代替,你永远猜不到发生了什么。它工作得非常好。因此,我唯一能想到的是,当我们将值保存到核心数据属性时,它没有正确保存。这很奇怪,因为创建和更新的属性都是NSString。实际上,核心数据实体中的所有内容都是NSString。最可能的解释是,字典返回的对象类与实体预期的对象不匹配。例如,
noteUpdated
应该是一个日期,但代码中没有强制执行该日期的内容。如果试图使用无法解析的字符串值设置日期,则该日期可能会报告值为零。您应该检查字典返回的类,看看它们是否适合分配给它们的属性
此外,这种描述的使用是不必要的:
NSLog(@"%@",[[managedObject valueForKey:@"title"] description]);
。。。因为NSLog仍然发送描述消息。这里是由description返回的字符串的描述。可能准确反映或可能不准确反映对象的值。只需使用:
NSLog(@"%@",[managedObject valueForKey:@"title"]);
排除故障时,我建议直接打印托管对象,如下所示:
NSLog(@"%@",manageObject);
。。。这会让你在更原始的状态下看到一切 最可能的解释是字典返回的对象类与实体预期的对象不匹配。例如,
noteUpdated
应该是一个日期,但代码中没有强制执行该日期的内容。如果试图使用无法解析的字符串值设置日期,则该日期可能会报告值为零。您应该检查字典返回的类,看看它们是否适合分配给它们的属性
此外,这种描述的使用是不必要的:
NSLog(@"%@",[[managedObject valueForKey:@"title"] description]);
。。。因为NSLog仍然发送描述消息。这里是由description返回的字符串的描述。可能准确反映或可能不准确反映对象的值。只需使用:
NSLog(@"%@",[managedObject valueForKey:@"title"]);
排除故障时,我建议直接打印托管对象,如下所示:
NSLog(@"%@",manageObject);
。。。这会让你在更原始的状态下看到一切 好吧,我不是CoreData的最佳人选,但在我看来,你两次提取数据。看起来您尝试拉两次更新的字符串。我怀疑这是个错误,但看起来你没有弄错。此外,您还可以缩短此'UIApplication*app=[UIApplication alloc];app.networkActivityIndicatorVisible=是;'截至此日期,“[[UIApplication shared application]SetNetworkActivityIndicator可见:是];我想这就是它或它的[UIApplication sharedApplication]的情况;好吧,我不是CoreData的最佳人选,但在我看来,你两次提取数据。看起来您尝试拉两次更新的字符串。我怀疑这是个错误,但看起来你没有弄错。此外,您还可以缩短此'UIApplication*app=[UIApplication alloc];app.networkActivityIndicatorVisible=是;'截至此日期,“[[UIApplication shared application]SetNetworkActivityIndicator可见:是];我想这就是它或它的[UIApplication sharedApplication]的情况;但是,如果我使用“创建的”valueForKey,问题就会减少。这与拉音符的方式完全相同。奇怪的是,一个不能工作,而另一个却能完美工作,这毫无意义。首先要消除可能的日志错误。只要直接记录对象本身,就会显示对象保存的所有属性/属性值。如果它们在那里显示正确,那么这只是一个日志错误。如果不是,那么问题是在哪里设置属性/属性值;它喜欢说出以下错误。在抛出“NSException”程序接收到的信号“SIGABRT”的实例后调用terminate。所以我看不到到底发生了什么…如果你有一个managedObject,你应该能够直接记录它。如果你仍然犯了错误,那么你就完全有别的事情在发生。嗯,非常奇怪的事情在发生。但是,如果我使用“创建的”valueForKey,这个问题就会减少。这与拉音符的方式完全相同。奇怪的是,一个不能工作,而另一个却能完美工作,这毫无意义。首先要消除可能的日志错误。只要直接记录对象本身,就会显示对象保存的所有属性/属性值。如果它们在那里显示正确,那么这只是一个日志错误。如果不是,那么问题是在哪里设置属性/属性值;它喜欢说出以下错误。在thr后终止呼叫