访问Managed对象的NSString属性

时间:2014-04-04 19:21:27

标签: ios objective-c core-data restkit restkit-0.20

我正在使用核心数据,Restkit开发应用程序并拥有一个简单的托管对象,如下所示 -

@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请求/响应被附加到它。如果我终止该应用并重新启动它,我就会得到美国/太平洋地区的#39;不确定为什么?

感谢您的帮助。

设置时区的代码是 -

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;
}

1 个答案:

答案 0 :(得分:0)

您看到的日志来自RestKit在映射期间使用的代理对象。它实际上没有保存到商店中,它只是在映射期间使用。您无需担心它,因为您发送给代理的任何消息都将转发到真正的基础项(在本例中为纯字符串) - 除了描述,因此您会看到详细的输出。

当您杀死应用程序时,您正在从数据存储中重新获取该项目,以便获得真正的基础数据,减去代理。