2014-01-27 77 views
3

所以我們可以說我有一個JSON對象,像這樣:Restkit 0.20與其他脫機數據一起JSON映射

{ 
    "userList" : [ 
    { 
     "ID" : 1, 
     "firstName" : "John", 
     "lastName" : "Doe" 
    }, 
    { 
     "ID" : 2, 
     "firstName" : "Jane", 
     "lastName" : "Doe" 
    } 
    ] 
} 

我能對這個對象映射到我的user類具有以下屬性:

ID, 
firstName, 
lastName, 
createdDate, 
modifiedData 

問題出現在我需要更新modified date我希望能夠插入一個數據時間標記,每當我做一個映射以及當我在離線模式下修改數據時。

所以我的問題是,如何將JSON對象映射到核心數據,同時還插入一些不存在於JSON對象中的數據。這甚至有可能嗎?

================

我的映射功能,如果它可以幫助:

+ (RKObjectMapping *)mapping { 
    // Create a singleton instance of the mapping object. 
    __strong static RKEntityMapping *_mapping = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     RKManagedObjectStore *store = [[RKObjectManager sharedManager] managedObjectStore]; 
     _mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:store]; 

     // Map attributes with the same name in the JSON and the model. 
     [_mapping addAttributeMappingsFromDictionary:@{@"ID": @"ID", 
                 @"firstName" : @"firstName", 
                 @"lastName" : @"lastName"}]; 

     // Set primaryKeyAttribute 
     _mapping.identificationAttributes = @[@"ID"]; 
    }); 
    return _mapping; 
} 

回答

0

處理此之外RestKit,但在某種程度上是由RestKit觸發(以及任何其他更改):

覆蓋您管理的對象子類上的willSave,並在調用時更新修改的日期(設置原始值以避免遞歸)。

+0

' - willSave'會導致一些髒的上下文。根據Paul Lange的這個SO回答(http://stackoverflow.com/questions/4874193/core-data-willsave-method),我應該重寫'load'方法並使用'NSNotification'。但是,謝謝@Wain,你幫助我從哪裏開始尋找。 – Steven

+0

這是一個非常好的和一般的解決方案,我喜歡它。 – Wain