0

当应用程序从 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]);
           }
      // }

    }
}
4

2 回答 2

0

您不能stringWithFormat在 内使用predicateWithFormat。您的谓词应该如下所示:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id=%@ and event_id=%@",
    [NSNumber numberWithInt:[[dictEvent valueForKey:@"user_id"] intValue]],
    [NSNumber numberWithInt:[[dictEvent valueForKey:@"id"] intValue]]
];
于 2012-10-06T13:30:14.367 回答
0

除了按照@Martin 的建议修改您的谓词

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id=%@ && event_id=%@",
    [NSNumber numberWithInt:[[dictEvent valueForKey:@"user_id"] intValue]],
    [NSNumber numberWithInt:[[dictEvent valueForKey:@"id"] intValue]]
];

请注意,在两种情况下,您正在使用不匹配的键更新您的对象:id 和 event_id,以及受邀状态和邀请状态。

于 2012-10-06T13:40:48.763 回答