2014-09-29 36 views
0

不刪除本地對象按照文檔爲0.20 RK:RESTKit DELETE請求在2xx的成功

RKManagedObjectRequestOperation adds special behavior to DELETE requests. Upon retrieving a successful (2xx status code) response for a DELETE, the operation will invoke deleteObject: with the operations targetObject on the managed object context. This will delete the target object from the local store in conjunction the successfully deleted remote representation. 

我一直在試圖刪除對象這樣的要求,但不管我怎麼努力我不能似乎得到它的工作。我成功地執行了許多對象的請求,這些對象被映射到適當的類並存儲在覈心數據中。當我嘗試對其中一個對象執行刪除請求並取回200成功時,它不會從本地存儲中刪除。

這裏有一些代碼,我毫無疑問錯過了一個把戲。

AppDelegate.m

... 
// 
// Match Mapping 
// 
RKEntityMapping *matchMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Match class]) 
                 inManagedObjectStore:objectManager.managedObjectStore]; 
NSDictionary *matchAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
             @"objectId", @"id", 
              @"score", @"matchScore", 
              @"date", @"matchDate", 
              nil]; 
matchMapping.identificationAttributes = @[@"objectId"]; 
[matchMapping addAttributeMappingsFromDictionary:matchAttributes]; 

// Response descriptor for GET 
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:matchMapping 
                        method:RKRequestMethodGET 
                      pathPattern:@"match/" 
                       keyPath:@"matches" 
                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]; 
// Response Descriptor for PUT 
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:matchMapping 
                        method:RKRequestMethodPUT 
                      pathPattern:@"match/" 
                       keyPath:@"match" 
                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]; 

// Request Descriptor for DELETE 
[objectManager addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:[matchMapping inverseMapping] 
                      objectClass:[Match class] 
                      rootKeyPath:nil 
                       method:RKRequestMethodDELETE]]; 

MatchDetailVC.m

... 
- (void)deleteMatch { 

NSDictionary *requiredParameters = @{ 
            @"APIKey": @"xxxxx" 
            }; 

NSMutableURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:self.match 
                      method:RKRequestMethodDELETE 
                      path:@"match/" 
                     parameters:requiredParameters]; 

RKManagedObjectRequestOperation *operation = [[RKObjectManager sharedManager] 
               managedObjectRequestOperationWithRequest:request 
               managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext 
               success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
                //[[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext save:nil]; // IS THIS NEEDED? 
                NSLog(@"Successfully deleted match."); 
                [self.navigationController popToRootViewControllerAnimated:YES]; 
               } 
               failure:^(RKObjectRequestOperation *operation, NSError *error) { 
                NSLog(@"Error: %@", [error localizedDescription]); 
               }]; 

NSOperationQueue *operationQueue = [NSOperationQueue new]; 
[operationQueue addOperation:operation]; 
} 
... 

在此先感謝,如果你需要更多的代碼,讓我知道。 安迪

+0

我已經設置了'operation.targetObject = self.match'但仍然沒有喜悅。 RKLog確實表示該對象正在被刪除,但在觀看SQLite數據庫時沒有任何變化。 – Andy 2014-09-29 17:43:23

回答

0

我知道這是一個很老的文章,但這裏是我發現了尋找年齡後......

本地刪除,如果沒有爲DELETE響應沒有有效響應映射將失敗。 問題wen't離開了我,當我創建了一個空的響應這樣的映射:

RKObjectMapping* nullMapping = [RKObjectMapping mappingForClass:[NSNull class]]; 
    [objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:nullMapping method:RKRequestMethodDELETE pathPattern:@"mybase/something/:myid/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];