Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NSManagedObjectContext *backgroundContext = [[[NSManagedObjectContext alloc] init] autorelease];
- [backgroundContext setPersistentStoreCoordinator:[[NSManagedObjectContext defaultContext] persistentStoreCoordinator]];
- for (int i = 0; i < [tweets count]; i++) {
- NSDictionary *tweetDict = [tweets objectAtIndex:i];
- Tweet *tweet = [Tweet createInContext:backgroundContext];
- tweet.text = [tweetDict objectForKey:@"text"];
- NSDateFormatter *df = [[NSDateFormatter alloc] init];
- [df setTimeStyle:NSDateFormatterFullStyle];
- [df setFormatterBehavior:NSDateFormatterBehavior10_4];
- [df setDateFormat:@"EEE, d LLL yyyy HH:mm:ss Z"];
- NSDate *date = [df dateFromString:[tweetDict objectForKey:@"created_at"]];
- [df release];
- tweet.date = date;
- tweet.uid = [tweetDict objectForKey:@"id"];
- NSDictionary *friend = [tweetDict objectForKey:@"user"];
- User *user = [User findFirstByAttribute:@"uid" withValue:[friend objectForKey:@"id"]];
- if (user != nil) {
- [tweet setValue:[[NSManagedObjectContext defaultContext] objectWithID:[user objectID]] forKey:@"user"];
- } else {
- user = [User findFirstByAttribute:@"uid" withValue:[friend objectForKey:@"id"] inContext:backgroundContext];
- if (user == nil) {
- User *newUser = [User createInContext:backgroundContext];
- newUser.uid = [friend objectForKey:@"id"];
- newUser.name = [friend objectForKey:@"name"];
- newUser.screenName = [friend objectForKey:@"screen_name"];
- newUser.profileImageURL = [friend objectForKey:@"profile_image_url"];
- if (![[friend objectForKey:@"description"] isKindOfClass:[NSNull class]]) {
- newUser.about = [friend objectForKey:@"description"];
- }
- tweet.user = newUser;
- } else {
- tweet.user = user;
- }
- }
- }
- [backgroundContext saveOnMainThread];
Add Comment
Please, Sign In to add comment