RestKit - RKObjectMapping 只运行一次

Posted

技术标签:

【中文标题】RestKit - RKObjectMapping 只运行一次【英文标题】:RestKit - RKObjectMapping only runs once 【发布时间】:2012-01-30 01:39:22 【问题描述】:

我正在使用 RestKit ios 包来允许用户从他们拥有的帐户中提取信息。下面的代码第一次完美运行。但是,如果我第二次尝试使用不同的用户名和密码,它只会加载第一次映射调用中的信息。此外,当第二次发送请求时,它会以不合理的速度返回数据,这让我相信它只是在使用缓存数据。我已经到处搜索了重置restkit(及其缓存)的任何方法,但都没有奏效。这是我的代码:

- (void)login 
    RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[MyClass class]];
    [mapping mapKeyPathsToAttributes:
     @"Data.MailboxId", @"MailboxId",
     @"Data.value1", @"Value1",
     @"Data.value2", @"Value2",
     nil];


    [RKObjectManager objectManagerWithBaseURL:@"https://my.site.com"];

    [RKObjectManager sharedManager].client.username = username.text;
    [RKObjectManager sharedManager].client.password = password.text;

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/api/v1.0/?format=json" objectMapping:mapping delegate:self];

这是我在 RestKit 中的日志:

2012-01-30 11:59:40.413 HostedNumbers[6909:707] D restkit.network:RKClient.m:265 Reachability observer changed for client <RKClient: 0x6f09640>, suspending queue <RKRequestQueue: 0x6f0a5d0 name=(null) suspended=YES requestCount=0 loadingCount=0/5> until reachability to host '0.0.0.0' can be determined
2012-01-30 11:59:40.425 HostedNumbers[6909:707] D restkit.network:RKClient.m:389 Reachability to host '0.0.0.0' determined for client <RKClient: 0x6f09640>, unsuspending queue <RKRequestQueue: 0x6f0a5d0 name=(null) suspended=YES requestCount=1 loadingCount=0/5>
2012-01-30 11:59:40.436 HostedNumbers[6909:707] D restkit.network:RKRequest.m:362 Sending asynchronous GET request to URL https://my.hostednumbers.com/api/v1.0/Mailbox/?format=json.
2012-01-30 11:59:40.445 HostedNumbers[6909:707] T restkit.network:RKRequest.m:310 Prepared GET URLRequest '<NSMutableURLRequest https://my.hostednumbers.com/api/v1.0/Mailbox/?format=json>'. HTTP Headers: 
    Accept = "application/json";
    "Content-Length" = 0;
. HTTP Body: .
2012-01-30 11:59:43.045 HostedNumbers[6909:707] D restkit.network:RKResponse.m:163 Asked if canAuthenticateAgainstProtectionSpace: with authenticationMethod = NSURLAuthenticationMethodServerTrust
2012-01-30 11:59:43.513 HostedNumbers[6909:707] D restkit.network:RKResponse.m:163 Asked if canAuthenticateAgainstProtectionSpace: with authenticationMethod = NSURLAuthenticationMethodHTTPDigest
2012-01-30 11:59:43.516 HostedNumbers[6909:707] D restkit.network:RKResponse.m:137 Received authentication challenge
2012-01-30 11:59:44.066 HostedNumbers[6909:707] D restkit.network:RKResponse.m:163 Asked if canAuthenticateAgainstProtectionSpace: with authenticationMethod = NSURLAuthenticationMethodServerTrust
2012-01-30 11:59:45.914 HostedNumbers[6909:707] D restkit.network:RKResponse.m:196 NSHTTPURLResponse Status Code: 200
2012-01-30 11:59:45.917 HostedNumbers[6909:707] D restkit.network:RKResponse.m:197 Headers: 
    "Cache-Control" = "no-cache";
    "Content-Length" = 2733;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Mon, 30 Jan 2012 16:59:46 GMT";
    Expires = "-1";
    Pragma = "no-cache, no-cache";
    Server = "Microsoft-IIS/6.0";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";

2012-01-30 11:59:45.923 HostedNumbers[6909:707] T restkit.network:RKResponse.m:202 Read response body: "Data":["MailboxId":"SP6s2m5zgwze47lsN5bBew2","PhoneNumberList":["PhoneNumber":"1-877-564-7397","Description":"(877) 564-7397","Provisioned":true,"PhoneNumberId":"F5MOV_sc3-f9xWcAOCC1Tg2"],"PhoneNumber":"1-877-564-7397","Prefix":795,"Extension":4920,"ProductId":"_iFmgCPNtTuU1PlknnSqXQ2","ProductCode":"SAR","ProductDescription":"PATLive Messaging Executive","MailboxStatus":1,"BillingStatus":0,"MainMailbox":false,"InfoCode":0,"AliasId":"","FeatureGroupId":"hTw_mObs0ARWkbHxRXZyEw2","FeatureGroupDescription":"Hosted Voicemail/Fax","FeatureGroupIcon":"https://my.hostednumbers.com/images/services/SAR.jpg","ServiceName":"Hosted Voicemail/Fax","MailboxName":"Connor Grady 1","DID":true,"ExtensionDigits":0,"ParentMailboxId":"","NewMessages":4,"SavedMessages":2,"NewVoiceMessages":2,"SavedVoiceMessages":2,"NewFaxMessages":2,"SavedFaxMessages":0,"NewRecordings":0,"SavedRecordings":0,"MailboxId":"vEqAb2JRNLrP1lB51FVkEw2","PhoneNumberList":["PhoneNumber":"1-850-523-3762","Description":"(850) 523-3762","Provisioned":true,"PhoneNumberId":"T-Ykotn8UNweSr4O0jHiqw2","PhoneNumber":"1-888-330-3948","Description":"(888) 330-3948","Provisioned":true,"PhoneNumberId":"KK41mHIFEo5jD9DrzG8_Ig2","PhoneNumber":"1-888-682-8469","Description":"(888) 682-8469","Provisioned":true,"PhoneNumberId":"svD01yJqBrZuJK-HzKvpcQ2","PhoneNumber":"1-888-826-2674","Description":"(888) 826-2674","Provisioned":true,"PhoneNumberId":"zUfxFyXxKXtHB9nMdCs5lg2","PhoneNumber":"1-888-853-4264","Description":"(888) 853-4264","Provisioned":true,"PhoneNumberId":"vmMwo_7x2hYxKa4gAc0PpQ2","PhoneNumber":"1-888-856-2307","Description":"(888) 856-2307","Provisioned":true,"PhoneNumberId":"ViDGc7pJW25kkimk-ObguA2","PhoneNumber":"1-888-856-2742","Description":"(888) 856-2742","Provisioned":true,"PhoneNumberId":"Wpt7fnGXoz8RxcP-xZkl9g2","PhoneNumber":"1-888-858-8276","Description":"(888) 858-8276","Provisioned":true,"PhoneNumberId":"hB-cyJHU-w4BcCVcXmcIsw2"],"PhoneNumber":"1-850-523-3762","Prefix":795,"Extension":4613,"ProductId":"mLVv-rU2-6GRboN46onbOg2","ProductCode":"CTS","ProductDescription":"Call Tracker Service","MailboxStatus":1,"BillingStatus":0,"MainMailbox":false,"InfoCode":0,"AliasId":"","FeatureGroupId":"v1uO_bCjMNSP9mF90AlO4w2","FeatureGroupDescription":"Hosted Local","FeatureGroupIcon":"https://my.hostednumbers.com/images/services/CTS.jpg","ServiceName":"Hosted Local","MailboxName":"Connor Grady 2","DID":true,"ExtensionDigits":0,"ParentMailboxId":"","NewMessages":0,"SavedMessages":0,"NewVoiceMessages":0,"SavedVoiceMessages":0,"NewFaxMessages":0,"SavedFaxMessages":0,"NewRecordings":0,"SavedRecordings":0],"ErrorCode":0,"ErrorMessage":"","DataInfo":"TotalResults":2,"TotalPages":1,"CurrentPage":1,"ResultsPerPage":20
2012-01-30 11:59:45.956 HostedNumbers[6909:5e0f] D restkit.network:RKObjectLoader.m:210 Found directly configured object mapping, creating temporary mapping provider for keyPath '%@'

--我的退出发生在这里,以下是我的第二个请求,其中包含用户名和密码,通常应该返回一个身份验证质询--

2012-01-30 11:59:57.678 HostedNumbers[6909:707] D restkit.network:RKClient.m:265 Reachability observer changed for client <RKClient: 0x6f14400>, suspending queue <RKRequestQueue: 0x6f145b0 name=(null) suspended=YES requestCount=0 loadingCount=0/5> until reachability to host '0.0.0.0' can be determined
2012-01-30 11:59:57.687 HostedNumbers[6909:707] D restkit.network:RKRequest.m:362 Sending asynchronous GET request to URL https://my.hostednumbers.com/api/v1.0/Mailbox/?format=json.
2012-01-30 11:59:57.691 HostedNumbers[6909:707] T restkit.network:RKRequest.m:310 Prepared GET URLRequest '<NSMutableURLRequest https://my.hostednumbers.com/api/v1.0/Mailbox/?format=json>'. HTTP Headers: 
    Accept = "application/json";
    "Content-Length" = 0;
. HTTP Body: .
2012-01-30 11:59:57.698 HostedNumbers[6909:707] D restkit.network:RKClient.m:250 Reachability observer changed for RKClient <RKClient: 0x6f14400>, disposing of previous instance: <RKReachabilityObserver: 0x6f15cc0 host=0.0.0.0 isReachabilityDetermined=NO isMonitoringLocalWiFi=565500 reachabilityFlags=-- ------->
2012-01-30 12:00:00.012 HostedNumbers[6909:707] D restkit.network:RKResponse.m:163 Asked if canAuthenticateAgainstProtectionSpace: with authenticationMethod = NSURLAuthenticationMethodServerTrust
2012-01-30 12:00:02.076 HostedNumbers[6909:707] D restkit.network:RKResponse.m:196 NSHTTPURLResponse Status Code: 200
2012-01-30 12:00:02.082 HostedNumbers[6909:707] D restkit.network:RKResponse.m:197 Headers: 
    "Cache-Control" = "no-cache";
    "Content-Length" = 2733;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Mon, 30 Jan 2012 17:00:02 GMT";
    Expires = "-1";
    Pragma = "no-cache, no-cache";
    Server = "Microsoft-IIS/6.0";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";

2012-01-30 12:00:02.087 HostedNumbers[6909:707] T restkit.network:RKResponse.m:202 Read response body: "Data":["MailboxId":"SP6s2m5zgwze47lsN5bBew2","PhoneNumberList":["PhoneNumber":"1-877-564-7397","Description":"(877) 564-7397","Provisioned":true,"PhoneNumberId":"F5MOV_sc3-f9xWcAOCC1Tg2"],"PhoneNumber":"1-877-564-7397","Prefix":795,"Extension":4920,"ProductId":"_iFmgCPNtTuU1PlknnSqXQ2","ProductCode":"SAR","ProductDescription":"PATLive Messaging Executive","MailboxStatus":1,"BillingStatus":0,"MainMailbox":false,"InfoCode":0,"AliasId":"","FeatureGroupId":"hTw_mObs0ARWkbHxRXZyEw2","FeatureGroupDescription":"Hosted Voicemail/Fax","FeatureGroupIcon":"https://my.hostednumbers.com/images/services/SAR.jpg","ServiceName":"Hosted Voicemail/Fax","MailboxName":"Connor Grady 1","DID":true,"ExtensionDigits":0,"ParentMailboxId":"","NewMessages":4,"SavedMessages":2,"NewVoiceMessages":2,"SavedVoiceMessages":2,"NewFaxMessages":2,"SavedFaxMessages":0,"NewRecordings":0,"SavedRecordings":0,"MailboxId":"vEqAb2JRNLrP1lB51FVkEw2","PhoneNumberList":["PhoneNumber":"1-850-523-3762","Description":"(850) 523-3762","Provisioned":true,"PhoneNumberId":"T-Ykotn8UNweSr4O0jHiqw2","PhoneNumber":"1-888-330-3948","Description":"(888) 330-3948","Provisioned":true,"PhoneNumberId":"KK41mHIFEo5jD9DrzG8_Ig2","PhoneNumber":"1-888-682-8469","Description":"(888) 682-8469","Provisioned":true,"PhoneNumberId":"svD01yJqBrZuJK-HzKvpcQ2","PhoneNumber":"1-888-826-2674","Description":"(888) 826-2674","Provisioned":true,"PhoneNumberId":"zUfxFyXxKXtHB9nMdCs5lg2","PhoneNumber":"1-888-853-4264","Description":"(888) 853-4264","Provisioned":true,"PhoneNumberId":"vmMwo_7x2hYxKa4gAc0PpQ2","PhoneNumber":"1-888-856-2307","Description":"(888) 856-2307","Provisioned":true,"PhoneNumberId":"ViDGc7pJW25kkimk-ObguA2","PhoneNumber":"1-888-856-2742","Description":"(888) 856-2742","Provisioned":true,"PhoneNumberId":"Wpt7fnGXoz8RxcP-xZkl9g2","PhoneNumber":"1-888-858-8276","Description":"(888) 858-8276","Provisioned":true,"PhoneNumberId":"hB-cyJHU-w4BcCVcXmcIsw2"],"PhoneNumber":"1-850-523-3762","Prefix":795,"Extension":4613,"ProductId":"mLVv-rU2-6GRboN46onbOg2","ProductCode":"CTS","ProductDescription":"Call Tracker Service","MailboxStatus":1,"BillingStatus":0,"MainMailbox":false,"InfoCode":0,"AliasId":"","FeatureGroupId":"v1uO_bCjMNSP9mF90AlO4w2","FeatureGroupDescription":"Hosted Local","FeatureGroupIcon":"https://my.hostednumbers.com/images/services/CTS.jpg","ServiceName":"Hosted Local","MailboxName":"Connor Grady 2","DID":true,"ExtensionDigits":0,"ParentMailboxId":"","NewMessages":0,"SavedMessages":0,"NewVoiceMessages":0,"SavedVoiceMessages":0,"NewFaxMessages":0,"SavedFaxMessages":0,"NewRecordings":0,"SavedRecordings":0],"ErrorCode":0,"ErrorMessage":"","DataInfo":"TotalResults":2,"TotalPages":1,"CurrentPage":1,"ResultsPerPage":20
2012-01-30 12:00:02.115 HostedNumbers[6909:3833] D restkit.network:RKObjectLoader.m:210 Found directly configured object mapping, creating temporary mapping provider for keyPath '%@'

【问题讨论】:

您会发布 RestKit 日志以验证是否应归咎于缓存吗? 将 RestKit 日志添加到原始帖子^ 【参考方案1】:

答案出奇地简单。我刚刚将以下代码行添加到我的请求中以强制进行基本 HTTP 身份验证,并且一切正常:

[RKObjectManager sharedManager].client.authenticationType = RKRequestAuthenticationTypeHTTPBasic;

【讨论】:

【参考方案2】:

您是否尝试过更改缓存策略?

f.e.

RKClient* client = [RKClient clientWithBaseURL:APP_URL];
client.cachePolicy = RKRequestCachePolicyNone;

试试看这是否能解决您的问题(暂时)。

【讨论】:

我在我的代码中添加了以下内容:[RKObjectManager sharedManager].client.cachePolicy = RKRequestCachePolicyNone;不幸的是,问题仍然存在。

以上是关于RestKit - RKObjectMapping 只运行一次的主要内容,如果未能解决你的问题,请参考以下文章

RestKit RKObjectMapping Swift 可选

RestKit RKObjectMapping 嵌套 json

CocoaPods 更新 RestKit #import "RKObjectMapping.h" 文件未找到

如何使用带有 RKObjectMapping 的 RestKit 0.24 将本地 JSON 字符串映射到对象?

使用 RKParams 向 API 发布帖子并使用 RestKit 使用 RKObjectMapping 映射响应

RestKit:给定 RKObjectMapping、object.class 和 JSON 字符串,如何获取映射对象?