我在数据库中有一些消息,我每次都会从该用户的服务器获取全部消息。我必须比较两个数组并且需要过滤新消息。每条消息都有一个唯一的 id,即 messageId
NSMutableArray *arrayMessagesServer = [response objectForKey:@"messageList"];
arrayMessagesDB=[[NSMutableArray alloc]initWithArray:[self getMessagesFromDB]];
for (int i = 0; i<[arrayMessagesServer count]; i++)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"messageId == %i", [[arrayMessagesServer objectAtIndex:i] integerValue]];
//If message not exists, saving message to DB
if(![arrayMessagesDB filteredArrayUsingPredicate:predicate])
{
NSDictionary *dict = [arrayForMessagesInServer objectAtIndex:i];
Messages *objMessages = [NSEntityDescription insertNewObjectForEntityForName:@"Messages" inManagedObjectContext:context];
[objMessages setValue:[dict objectForKey:@"message"] forKey:@"messageBody"];
[objMessages setValue:[dict objectForKey:@"messageId"] forKey:@"messageId"];
[objMessages setValue:[NSString stringWithFormat:@"%@",[dict objectForKey:@"messageType"]] forKey:@"messageType"];
}
}
NSError *error1;
if (![context save:&error1])
{
NSLog(@"Problem saving: %@", [error1 localizedDescription]);
}