Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)contextDidChange:(NSNotification *)notif {
  2.   NSLog(@"Context Did Change: %@", notif);
  3.   NSMutableSet *insertedObjects = [NSMutableSet setWithSet:[[notif userInfo] valueForKey:NSInsertedObjectsKey]];
  4.   NSMutableSet *updatedObjects = [NSMutableSet setWithSet:[[notif userInfo] valueForKey:NSUpdatedObjectsKey]];
  5.  
  6.   [updatedObjects enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
  7.     CacheObject * object = obj;
  8.     if ([object id] == 0) {
  9.       [insertedObjects addObject:obj];
  10.       return;
  11.     }
  12.     if (object.dirty) {
  13.       MFormRequest *request = [MFormRequest requestForLocalObject:object];
  14.      
  15.       request.requestMethod = @"PUT";
  16.       [request setPostCacheObject:object forKey:[NSStringFromClass([object class]) lowercaseString]];
  17.       [request startAsynchronous];      
  18.     }
  19.   }];
  20.  
  21.   [insertedObjects enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
  22.     CacheObject * object = obj;
  23.     if ([[object id] intValue] == 0) {
  24.       NSString *resource = [[NSStringFromClass([object class]) lowercaseString] stringByAppendingString:@"s"];
  25.       MFormRequest *request = [MFormRequest requestWithPath:resource];
  26.       request.userInfo = [NSDictionary dictionaryWithObject:object forKey:kMManagedObjectContextNewObject];
  27.      
  28.       request.requestMethod = @"POST";
  29.       [request setPostCacheObject:object forKey:[NSStringFromClass([object class]) lowercaseString]];
  30.       [request setCompletionBlock:^ {
  31.         CacheObject *cacheObject = [request.userInfo valueForKey:kMManagedObjectContextNewObject];
  32.         NSDictionary *dict = [[[request responseString] JSONValue] valueForKey:[NSStringFromClass([cacheObject class]) lowercaseString]];
  33.         [cacheObject updateWithDictionary:dict];
  34.       }];
  35.       [request startAsynchronous];      
  36.     }
  37.   }];  
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement