【问题标题】:Restkit: RKRequest delegates not calledRestkit:未调用 RKRequest 代表
【发布时间】:2013-01-14 21:14:53
【问题描述】:

我真的被这个问题难住了。我正在使用 Restkit 访问 Web 服务。大多数操作都可以正常工作,但前两个,initializeDevice 和 insertNewPatron,不是。对于这些,会触发异步请求,但从不调用委托方法。我已经在服务器上进行了检查,没有证据表明正在拨打这些电话。我不知道这些操作有什么不同使它们不起作用。这是进行调用的类:

-(id)initForController:(UIViewController *)viewController{
    self.vc=viewController;
    NSString *baseURL = @"https://xxxxxxxxx/APNS_WebService/rest/operations/";
    NSURL *url=[NSURL URLWithString:baseURL];

    client = [RKClient clientWithBaseURL:url];
    client.disableCertificateValidation=YES;

    return self;
}

-(void)insertNewPatronWithCard:(NSString *)cardNumber PIN:(NSString *)pin NickName:(NSString *)nickname Device:(NSString *)deviceID{


    request = [client requestWithResourcePath:[NSString stringWithFormat:@"/insertNewPatron?cardNumber=%@&pin=%@&nickname=%@&deviceID=%@",cardNumber,pin,nickname,deviceID]];
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"insertNewPatron" forKey:@"operation"];
    request.params=params;
    request.delegate=self;
    [request sendAsynchronously];


}

-(void)initializeDeviceWithID:(NSString *)deviceID{



    request = [client requestWithResourcePath:[NSString stringWithFormat:@"/initializeDevice?deviceID=%@",deviceID]];
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"initializeDevice" forKey:@"operation"];
    request.params=params;
    request.delegate=self;
    [request sendAsynchronously];


}

-(void)updateDevicePreferencesWithDeviceID:(NSString *)deviceID DueDateNotice:(NSString *)dueDateNotice HoldsNotice:(NSString *)holdsNotice EventNotice:(NSString *)eventNotice{


    request = [client requestWithResourcePath:[NSString stringWithFormat:@"/updateDevicePreferences?deviceID=%@&dueDateNotice=%@&holdsNotice=%@&eventNotice=%@",deviceID,dueDateNotice,holdsNotice,eventNotice]];
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"updateDevicePreferences" forKey:@"operation"];
    request.params=params;
    request.delegate=self;
    [request sendAsynchronously];


}

-(void)removeUserWithCard:(NSString *)cardNumber{


    request = [client requestWithResourcePath:[NSString stringWithFormat:@"/removeUser?cardNumber=%@",cardNumber]];
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"removeUser" forKey:@"operation"];
    request.params=params;
    request.delegate=self;
   [request sendAsynchronously];


}

-(void)addEventWithDevice:(NSString *)deviceID EventID:(NSString *)eventID{



    request = [client requestWithResourcePath:[NSString stringWithFormat:@"/addEvent?deviceID=%@&eventID=%@",deviceID,eventID]];
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"addEvent" forKey:@"operation"];
    request.params=params;
    request.delegate=self;
    [request send];


}

-(void)removeEventWithDevice:(NSString *)deviceID EventID:(NSString *)eventID{


   request = [client requestWithResourcePath:[NSString stringWithFormat:@"/removeEvent?deviceID=%@&eventID=%@",deviceID,eventID]];
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"removeEvent" forKey:@"operation"];
    request.params=params;
    request.delegate=self;
     [request sendAsynchronously];

}

-(void)removeAllEventsWithDevice:(NSString *)deviceID{


    request = [client requestWithResourcePath:[NSString stringWithFormat:@"/removeAllEvents?deviceID=%@",deviceID]];
    request.delegate=self;
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"removeAllEvents" forKey:@"operation"];
    request.params=params;

   [request sendAsynchronously];


}

我注意到,对于那些不起作用的操作,在 Restkit 日志记录中,我收到了“准备好 GET UrlRequest”的消息,但没有看到“正在处理请求”的消息那个工作。

【问题讨论】:

  • 您是否使用断点检查所有变量是否正确?没有 nil 代表或您缺少的东西?您是在隔离请求还是在另一个呼叫同时发出请求?您的一个电话很可能在此过程中失去了代表。我建议对异步请求使用块而不是委托范例以避免竞争条件。
  • 这是个好主意;我会尝试重新设计它以使用块。但是,由于服务器似乎没有收到请求,看来这一定是一个更深层次的问题。
  • 这似乎成功了,虽然我不完全明白为什么。将此作为答案,我会接受。
  • 你解决了它改变对块的调用?

标签: ios web-services delegates restkit


【解决方案1】:

您是否使用断点检查所有变量是否正确?没有零代表或您缺少的东西?您是在隔离请求还是在另一个呼叫同时发出请求?

您的一个电话很可能在此过程中失去了代表。我建议对异步请求使用 blocks 而不是委托范例以避免竞争条件。

希望对你有帮助!

【讨论】:

  • 它并没有真正解释为什么服务器没有收到呼叫,但是更改为块可以使其工作。另外,我现在看到,对于我想要做的事情来说,使用块是一种更好的设计方式。
  • 除了修复错误之外,使用块更好,因为可以提高代码的可读性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 2017-12-30
  • 2012-03-08
  • 2018-09-16
相关资源
最近更新 更多