2014-04-04 108 views
1

我工作的一個應用程序與核心數據,Restkit並有一個簡單的管理對象,如下的訪問的NSString財產 -管理對象

@interface MyManagedObject : NSManagedObject 
@property (nonatomic, strong) NSString *timeZone; 
@end 

我的問題是,當我嘗試訪問myManagedObj.timeZone ,我得到的值是 -

US/Pacific ({ 
    HTTP =  { 
     request =   { 
      URL = "https://myapp.com/getTimeZone"; 
      headers =    { 
       Accept = "application/json"; 
       "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5"; 
       "Content-Type" = "application/json; charset=utf-8"; 
       "User-Agent" = "MyApp/204 (iPhone Simulator; iOS 7.0.3; Scale/2.00)"; 
      }; 
      method = POST; 
     }; 
     response =   { 
      URL = "https://myapp.com/getTimeZone"; 
      headers =    { 
       "Access-Control-Allow-Origin" = "*"; 
       "Alternate-Protocol" = "443:quic"; 
       "Cache-Control" = "no-cache"; 
       "Content-Type" = "application/json"; 
       Date = "Fri, 04 Apr 2014 19:11:09 GMT"; 
       Expires = "Fri, 04 Apr 2014 19:11:09 GMT"; 
       Vary = "Accept-Encoding"; 
      }; 
     }; 
    }; 
    mapping =  { 
     collectionIndex = 2955515963; 
     rootKeyPath = "data.user_profile"; 
    }; 
}) 

我期待的值就是「美國/太平洋」,不知道爲什麼HTTP請求/響應追加到它。如果我終止應用程序並重新啓動它,我只會得到'美國/太平洋'。不知道爲什麼?

感謝您的任何幫助。

的代碼來設置時區是 -

NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:self method:RKRequestMethodPOST path:[NSString stringWithFormat:@"/getTimeZone/%@", loginTypeString] parameters:params]; 
[self prioritizedRequestOperationWithRequest:request 
          queuePriority:NSOperationQueuePriorityVeryHigh 
          threadPriority:1 
           targetObject:self 
            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
             myManagedObject = (MyManagedObject *)[mappingResult firstObject]; 
            } 
            failure:^(RKObjectRequestOperation *operation, NSError *error, UCNetworkRequestError *ucError) { 
            } 
         useManagedRequest:YES]; 

下面是MyManagedObject響應映射 -

+ (RKObjectMapping *)responseMapping { 
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"MyManagedObject" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore]; 
    [entityMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"timeZone"]]; // array of strings 
    return entityMapping; 
} 
+0

你可以在設置youObject.timeZone時共享代碼嗎? – Krivoblotsky

+0

更新了問題以共享數據設置的代碼。 – Subhash

回答

0

您所看到的日誌是從RestKit映射時使用代理對象。它實際上並沒有保存到商店中,它只是在映射過程中使用。您不必擔心,因爲您發送給代理的任何消息都將轉發到真實的基礎項目(本例中爲純字符串) - 除了描述之外,因此您會看到詳細的輸出。

當您終止應用程序時,您正在從數據存儲中重新獲取該項目,以便獲取真正的基礎數據,然後減去代理。

+0

謝謝。有道理。 – Subhash

+0

但是,我不明白這一點:MyManagedObject實例存儲在一個NSSet。因此,當我嘗試使用'[NSPredicate predicateWithFormat:@「timeZone ==%@」,@「US/Pacific」]對文件集進行歸檔時,結果集合就是空的!所以我必須實現的解決方法是使用forloop來迭代NSSet,然後將isEqualToString與循環中的每個set對象進行比較。 – Subhash

+0

使用'mappingResult'中返回的託管對象?改爲在數據存儲上的提取請求中運行謂詞。 – Wain