2014-03-27 119 views
2

我對我的應用程序使用了RestKit 0.20.3。它具有離線模式,所以我必須將對象存儲在本地coredata中。RestKit本地刪除對象,仍然出現在映射中

每當從服務器刪除一些對象時,它會發送JSON中的deleted = true屬性,我在與deletionPredicate的映射中處理該屬性。 這是我的映射。

- (RKEntityMapping *)meetingsMapping { 

    RKEntityMapping *meetingsMapping = [RKEntityMapping mappingForEntityForName:@"DBMeetings" inManagedObjectStore:objectManager.managedObjectStore]; 
    meetingsMapping.setDefaultValueForMissingAttributes = NO; 
    meetingsMapping.deletionPredicate = [NSPredicate predicateWithFormat:@"shouldBeDeleted = 1"]; 
    [meetingsMapping setModificationAttributeForName:@"updated_at"]; 
    meetingsMapping.identificationAttributes = @[@"id"]; 

    [meetingsMapping addAttributeMappingsFromDictionary:@{ 
                 @"id": @"id", 
                 @"title": @"title", 

                 @"start_date_human": @"start_date_human", 
                 @"start_time_human": @"start_time_human", 
                 @"finish_date_human": @"finish_date_human", 
                 @"finish_time_human": @"finish_time_human", 

                 @"lock": @"lock", 
                 @"location": @"location", 
                 @"sample": @"sample", 

                 @"deleted": @"shouldBeDeleted", 

                 @"created_at": @"created_at", 
                 @"updated_at": @"updated_at", 
                 @"follow_up_id": @"follow_up_id", 
                 @"total_topics": @"total_topics", 
                 }]; 

[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"tags" mapping:[self tagsMapping]]; 
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"required_participants" mapping:[self contactsMapping]]; 
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"optional_participants" mapping:[self contactsMapping]]; 
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"readonly_participants" mapping:[self contactsMapping]]; 
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"organizer" mapping:[self contactsMapping]]; 

return meetingsMapping; 

}

它運作良好,並且屬性是從本地CoreData刪除。

問題是方法getObjectsAtPath仍返回mappingResult參數中的所有對象。

​​

假設,如果5出的20個對象被刪除,mappingResult.array返回20條記錄不15. 5個刪除的記錄沒有數據。當我登錄mappingResult.array,第一條記錄打印如下:

2014-03-27 13:10:14.105 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc1cb820> (entity: DBMeetings; id: 0xc1a6a60 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809410> ; data: <fault>): the object has a temporary managed object ID. 
2014-03-27 13:10:14.105 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc5b2ed0> (entity: DBMeetings; id: 0xc5d1110 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809416> ; data: <fault>): the object has a temporary managed object ID. 
2014-03-27 13:10:14.106 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc58b6a0> (entity: DBMeetings; id: 0xc5d3de0 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809417> ; data: <fault>): the object has a temporary managed object ID. 
2014-03-27 13:10:14.106 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc5b50e0> (entity: DBMeetings; id: 0xc5d4af0 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809419> ; data: <fault>): the object has a temporary managed object ID. 
2014-03-27 13:10:14.107 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc1ed5a0> (entity: DBMeetings; id: 0xc1c84e0 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809422> ; data: <fault>): the object has a temporary managed object ID. 

如果我在DB檢查,只有15條記錄。沒關係。

如果我明確地從數據庫提取,它只返回15條記錄,這是確定

但在getObjectsAtPath回調,則mappingResult.array參數返回20條記錄。這不好。 :(

的問題是mappingResult.arraycount變成20和我UITableView顯示20行,5行沒有數據。

是否有任何其他的回調由RestKit準確返回15條記錄,所以我不會必須從數據庫中明確提取記錄?

回答

2

mappingResult正在返回已映射的對象,即它應該執行的操作。事實上,某些項目後來被刪除並沒有改變 - 事實上,您擁有所有的所需的信息,因爲您知道所有映射的對象以及哪些對象因此被刪除。

簡單的解決方案是使用NSFetchedResultsController爲您的表視圖提供數據源。它將正確處理更新,插入和刪除,並且不需要做額外的工作。

或者,您需要迭代mappingResult.array並過濾掉已刪除的項目。驗證的最好方法是使用檢查:

if (item.isDeleted || item.managedObjectContext == nil) { 
    // remove the now invalid item... 
} 
+0

謝謝@wain,我使用MagicalRecords,並將它用於從數據庫中再次提取。它正在返回正確的記錄。我想知道是否有其他方法可以實現這一目標。 – Khawar