Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)contextDidChange:(NSNotification *)notif {
- NSLog(@"Context Did Change: %@", notif);
- NSMutableSet *insertedObjects = [NSMutableSet setWithSet:[[notif userInfo] valueForKey:NSInsertedObjectsKey]];
- NSMutableSet *updatedObjects = [NSMutableSet setWithSet:[[notif userInfo] valueForKey:NSUpdatedObjectsKey]];
- [updatedObjects enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
- CacheObject * object = obj;
- if ([object id] == 0) {
- [insertedObjects addObject:obj];
- return;
- }
- if (object.dirty) {
- MFormRequest *request = [MFormRequest requestForLocalObject:object];
- request.requestMethod = @"PUT";
- [request setPostCacheObject:object forKey:[NSStringFromClass([object class]) lowercaseString]];
- [request startAsynchronous];
- }
- }];
- [insertedObjects enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
- CacheObject * object = obj;
- if ([[object id] intValue] == 0) {
- NSString *resource = [[NSStringFromClass([object class]) lowercaseString] stringByAppendingString:@"s"];
- MFormRequest *request = [MFormRequest requestWithPath:resource];
- request.userInfo = [NSDictionary dictionaryWithObject:object forKey:kMManagedObjectContextNewObject];
- request.requestMethod = @"POST";
- [request setPostCacheObject:object forKey:[NSStringFromClass([object class]) lowercaseString]];
- [request setCompletionBlock:^ {
- CacheObject *cacheObject = [request.userInfo valueForKey:kMManagedObjectContextNewObject];
- NSDictionary *dict = [[[request responseString] JSONValue] valueForKey:[NSStringFromClass([cacheObject class]) lowercaseString]];
- [cacheObject updateWithDictionary:dict];
- }];
- [request startAsynchronous];
- }
- }];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement