Last active
August 29, 2015 14:02
-
-
Save xiekw2010/0b7a969a6ca8852d1a39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//worst | |
NSArray *sessionsFromServer = ...; | |
NSManagedObjectContext *context = ...; | |
for (WWDCSession *aSession in sessionsFromServer) { | |
NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"WWDCSession"]; | |
req.predicate = [NSPredicate predicateWithFormat:@"sessionID == %@", aSession.sessionID]; | |
NSArray *results = [context executeFetchRequest:req error:nil]; | |
WWDCSession *existSesion = [results firstObject]; | |
if (!existSesion) { | |
//Insert a New One | |
}else { | |
// Do something with the existSession | |
} | |
} | |
//better | |
NSArray *sessionsFromServer = ...; | |
NSManagedObjectContext *context = ...; | |
NSArray *sessionIDs = [sessionsFromServer valueForKey:@"SessionID"]; | |
NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"WWDCSession"]; | |
req.predicate = [NSPredicate predicateWithFormat:@"sessionID in %@", sessionIDs]; | |
NSArray *results = [context executeFetchRequest:req error:nil]; | |
for (WWDCSession *aSession in sessionsFromServer) { | |
NSInteger existingSessionIndex = [results indexOfObject:aSession]; | |
if (existingSessionIndex == NSNotFound) { | |
//Insert a New One | |
}else { | |
// Do something with the existSession | |
} | |
} | |
//right | |
NSArray *sessionsFromServer = ...; | |
NSManagedObjectContext *context = ...; | |
NSArray *sessionIDs = [sessionsFromServer valueForKey:@"SessionID"]; | |
NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"WWDCSession"]; | |
req.predicate = [NSPredicate predicateWithFormat:@"sessionID in %@", sessionIDs]; | |
NSArray *results = [context executeFetchRequest:req error:nil]; | |
NSDictionary *mapDic = [NSDictionary dictionaryWithObjects:results forKeys:[results valueForkey:@"sessionID"]]; | |
for (WWDCSession *aSession in sessionsFromServer) { | |
WWDCSession *existingSession = mapDic[aSession.sessionID]; | |
if (!existingSession) { | |
//Insert a New One | |
}else { | |
// Do something with the existSession | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment