RestKit iOS:简单请求错误

发布于 2025-01-05 02:02:53 字数 1419 浏览 1 评论 0原文

我是 iOS 开发新手,目前正在使用 ARC 测试适用于 iOS 的 RestKit 0.9.3 和 xCode 4.2,我遇到了一些简单的 get 请求问题。

我遵循本教程: https://github.com/RestKit /RestKit/wiki/Tutorial-%3A-Introduction-to-RestKit

我尝试在 TouchUpInside a 上向我的 Web 服务发送一个简单的获取请求UIButton

但我收到一条“ EXC_BAD_ACCESS ”:[6373:fb03] *** -[DataAccess respondsToSelector:]: message sent to deallocated instance 0x8275160

应用程序在 RKRequest.m 上的这一行停止 文件:

if ([self.delegate respondsToSelector:@selector(requestDidStartLoad:)]) {
    [self.delegate requestDidStartLoad:self];
}

我的代码:

MyViewController.m:

- (IBAction)myAction:(id)sender {
    DataAccess *data = [DataAccess alloc];
    [data sendRequests];
}

DataAccess.m:

@implementation DataAccess

-(void)sendRequests {

    [RKClient clientWithBaseURL:SERVER_URL username:SERVER_USERNAME password:SERVER_PASSWORD];  
    [[RKClient sharedClient] get:@"/myDistantAction" delegate:self];
}

#pragma mark - Delegate

-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {

    if ([response isOK]) {
        NSLog(@"Retrieved : %@", [response bodyAsString]);
    }
}

@end

我在互联网上搜索,但没有找到解决方案

有人可以 帮我 ?

谢谢,

I'm newbie on iOS development and I'm currently testing RestKit 0.9.3 for iOS with xCode 4.2 using ARC and I encounter some problem for a simple get request.

I following this tutorial : https://github.com/RestKit/RestKit/wiki/Tutorial-%3A-Introduction-to-RestKit

I try to send a simple get request to my webservices on TouchUpInside a UIButton.

But I receive an " EXC_BAD_ACCESS " : [6373:fb03] *** -[DataAccess respondsToSelector:]: message sent to deallocated instance 0x8275160

The application stop at this line, on RKRequest.m file :

if ([self.delegate respondsToSelector:@selector(requestDidStartLoad:)]) {
    [self.delegate requestDidStartLoad:self];
}

My code :

MyViewController.m :

- (IBAction)myAction:(id)sender {
    DataAccess *data = [DataAccess alloc];
    [data sendRequests];
}

DataAccess.m :

@implementation DataAccess

-(void)sendRequests {

    [RKClient clientWithBaseURL:SERVER_URL username:SERVER_USERNAME password:SERVER_PASSWORD];  
    [[RKClient sharedClient] get:@"/myDistantAction" delegate:self];
}

#pragma mark - Delegate

-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {

    if ([response isOK]) {
        NSLog(@"Retrieved : %@", [response bodyAsString]);
    }
}

@end

I searched on the Internet but I didn't found the solution

Someone could help me ?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2025-01-12 02:02:53

这可能是一种解决方案。我更改了您的代码以使用单例。我认为问题是当调用回调函数时,因为他无法再访问该实例。

DataAccess.m:

@implementation DataAccess

static singleton *DataAccess= nil;

+ (DataAccess*)getInstance
{
    if (singleton == nil) {
        singleton = [[DataAccess alloc] init];
    }
    return singleton;
}

-(void)sendRequests {

    [RKClient clientWithBaseURL:SERVER_URL username:SERVER_USERNAME password:SERVER_PASSWORD];  
    [[RKClient sharedClient] get:@"/myDistantAction" delegate:self];
}

#pragma mark - Delegate

-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {

    if ([response isOK]) {
        NSLog(@"Retrieved : %@", [response bodyAsString]);
    }
}

@end

MyViewController.m:

- (IBAction)myAction:(id)sender {
    DataAccess *data = [DataAccess getInstance];
    [data sendRequests];
}

This may be one solution. I changed your code to use a singleton. I think the problem is when the callback function is called because he can no longer access the instance.

DataAccess.m:

@implementation DataAccess

static singleton *DataAccess= nil;

+ (DataAccess*)getInstance
{
    if (singleton == nil) {
        singleton = [[DataAccess alloc] init];
    }
    return singleton;
}

-(void)sendRequests {

    [RKClient clientWithBaseURL:SERVER_URL username:SERVER_USERNAME password:SERVER_PASSWORD];  
    [[RKClient sharedClient] get:@"/myDistantAction" delegate:self];
}

#pragma mark - Delegate

-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {

    if ([response isOK]) {
        NSLog(@"Retrieved : %@", [response bodyAsString]);
    }
}

@end

MyViewController.m:

- (IBAction)myAction:(id)sender {
    DataAccess *data = [DataAccess getInstance];
    [data sendRequests];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文