我尝试在从服务器收到成功响应后将数据插入CoreData。除了插入数据外,一切都很有效。有人可以看看我的片段,让我知道它是错还是可能缺少某些东西。 [items objectAtIndex:]返回值。除了我在下面的代码之后执行fetchedObjects count
之外,一切都工作了,它返回数据库中已有的值,而不是正在插入的值。
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *item = [operation.responseString componentsSeparatedByString:@"-"];
AppDelegate *ad = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [ad managedObjectContext];
NSManagedObject *object = [NSEntityDescription
insertNewObjectForEntityForName:@"Events"
inManagedObjectContext:context];
[object setValue:@"1" forKey:@"accounts"];
[object setValue:@"1" forKey:@"active"];
[object setValue:address forKey:@"address"];
[object setValue:ayn forKey:@"anonymous"];
[object setValue:Starttimestamp forKey:@"date"];
[object setValue:Endtimestamp forKey:@"enddate"];
[object setValue:[item objectAtIndex:3] forKey:@"eslink"];
[object setValue:[item objectAtIndex:6] forKey:@"eventID"];
[object setValue:[item objectAtIndex:5] forKey:@"eventprofile"];
[object setValue:@"" forKey:@"imagedata"];
[object setValue:@"0" forKey:@"images"];
[object setValue:@"0" forKey:@"isLive"];
[object setValue:lp forKey:@"lin_priv"];
[object setValue:Ename forKey:@"name"];
[object setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"Cloud_ID"] forKey:@"owner"];
[object setValue:pp forKey:@"pic_priv"];
[object setValue:[item objectAtIndex:1] forKey:@"pin"];
[object setValue:[item objectAtIndex:2] forKey:@"profile"];
[object setValue:[item objectAtIndex:4] forKey:@"username"];
NSError *error;
if (![context save:&error]) {
NSLog(@"Failed to save - error: %@", [error localizedDescription]);
}else{
NSLog(@"Success");
}
});
编辑:我添加了成功块以显示。