当应用程序从 Web 服务器获得响应时,我正在尝试更新我的本地数据库。当应用程序从 Web 服务器获取更新时,我通过将 id 与响应匹配来从本地数据库中获取数据并获取一行并执行更新代码,但本地数据库没有得到更新,也没有给出错误。应该怎么解决???
-(void)checkID:(NSMutableDictionary *)dict
{
NSDictionary *dictEvent = [dict objectForKey:@"Event"];
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *selectedManagedObject = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Events" inManagedObjectContext:context];
NSSortDescriptor *sortDescObj = [[NSSortDescriptor alloc] initWithKey:@"event_id" ascending:YES];
NSError *error = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"user_id=%@ and event_id=%@",[NSNumber numberWithInt:[[dictEvent valueForKey:@"user_id"] intValue]],[NSNumber numberWithInt:[[dictEvent valueForKey:@"id"] intValue]]]];
NSLog(@"Predicate = %@",predicate);
NSArray *arrSortDescriptors = [NSArray arrayWithObject:sortDescObj];
[fetchRequest setSortDescriptors:arrSortDescriptors];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setPredicate:predicate];
NSArray *arrResult = [context executeFetchRequest:fetchRequest error:&error];
if ([arrResult count]>0)
{
NSArray *arrKey = [dictEvent allKeys];
NSArray *arrValue = [dictEvent allValues];
NSLog(@"ArrKey : %@\nArrValue : %@",arrKey,arrValue);
selectedManagedObject = [arrResult objectAtIndex:0];
for(int i = 0; i < [arrKey count] ; i++)
{
NSLog(@"selectedMng :- %@",selectedManagedObject);
NSLog(@"KEY: %@\t: %@",[arrKey objectAtIndex:i],[arrValue objectAtIndex:i]);
if ([[arrKey objectAtIndex:i]isEqualToString:@"id"])
{
[selectedManagedObject setValue:[arrValue objectAtIndex:i] forKey:@"event_id"];
}
else if([[arrKey objectAtIndex:i]isEqualToString:@"invited_status"])
{
[selectedManagedObject setValue:[arrValue objectAtIndex:i] forKey:@"invite_status"];
}
else
{
[selectedManagedObject setValue:[arrValue objectAtIndex:i] forKey:[arrKey objectAtIndex:i]];
}
}
if (! [selectedManagedObject.managedObjectContext save:&error])
{
NSLog(@"updateEntityIntoDataBaseNamed - Error :: %@", [error localizedDescription]);
}
// }
}
}