当我在后台运行 NSURLConnection 错误时......我该如何修复?

发布于 2025-01-08 18:33:37 字数 1319 浏览 0 评论 0原文

我有这段代码,但我不明白为什么我必须在主线程上运行它。 如果我在后台运行它,它不会执行发布请求。这是一个错误吗? 我该如何解决这个问题?

- (void)setRead:(MWFeedItem *)entry
{    [self getToken:YES];

    NSString *url=[NSString stringWithFormat:@"https://www.google.com/reader/api/0/edit-tag?a=user/-/state/com.google/read&i=%@&T=%@", entry.identifier, token];

    [self postRequestWithURLState:url];

}
- (void)postRequestWithURLState:(NSString *)url
{
    NSString *bodyRequest = nil;
    NSURL *requestURL = [NSURL URLWithString:url];
    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];

    //NSLog(@"-------------- bodyRequest: %@", bodyRequest);



    [theRequest setURL:requestURL];
    [theRequest setTimeoutInterval:0.5];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[bodyRequest dataUsingEncoding:NSASCIIStringEncoding]];
    [self.oauthAuthentication authorizeRequest:theRequest];
    [NSURLConnection connectionWithRequest:theRequest delegate:self];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;



}

这是我的电话:

-(void)segnaLettura:(MWFeedItem *)item{

        [reader setRead:item];

}
- (void) segnaread:(MWFeedItem *)item{
    [self performSelectorOnMainThread:@selector(segnaLettura:) withObject:item waitUntilDone:NO];
}

I've this code but I can't understand why I must run this on the main thread.
If I run this in the background It doesn't do the post request. Is it a bug?
How can I solve this problem?

- (void)setRead:(MWFeedItem *)entry
{    [self getToken:YES];

    NSString *url=[NSString stringWithFormat:@"https://www.google.com/reader/api/0/edit-tag?a=user/-/state/com.google/read&i=%@&T=%@", entry.identifier, token];

    [self postRequestWithURLState:url];

}
- (void)postRequestWithURLState:(NSString *)url
{
    NSString *bodyRequest = nil;
    NSURL *requestURL = [NSURL URLWithString:url];
    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];

    //NSLog(@"-------------- bodyRequest: %@", bodyRequest);



    [theRequest setURL:requestURL];
    [theRequest setTimeoutInterval:0.5];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[bodyRequest dataUsingEncoding:NSASCIIStringEncoding]];
    [self.oauthAuthentication authorizeRequest:theRequest];
    [NSURLConnection connectionWithRequest:theRequest delegate:self];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;



}

this is my call:

-(void)segnaLettura:(MWFeedItem *)item{

        [reader setRead:item];

}
- (void) segnaread:(MWFeedItem *)item{
    [self performSelectorOnMainThread:@selector(segnaLettura:) withObject:item waitUntilDone:NO];
}

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

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

发布评论

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

评论(1

无法回应 2025-01-15 18:33:37

为了使异步 NSURLConnection 发挥作用,它需要处理线程的运行循环。虽然新线程会自动获得运行循环,但运行它取决于您。

您可以在线程中了解如何执行此操作编程指南,我可以对其进行更多解释,但大多数时候这不是您想要的。大多数时候,在 iOS 中,后台线程应该使用 NSOperation 或 GCD 进行管理。一般来说,如果您在 iOS 4+ 上手动生成线程,那么您就做错了。也有例外,但并不常见。

这里的第一个问题应该是“为什么我有一个后台线程?”

如果您确实需要后台线程,那么您执行segnaread的方式可能就可以了。

In order for an asynchronous NSURLConnection to function, it requires the thread's runloop to be processed. While new threads automatically get a runloop, it is up to you to run it.

You can learn how to do this in the Threading Programming Guide, and I can explain it more, but most of the time this isn't what you want. Most of the time in iOS, background threads should be managed with NSOperation or GCD. Generally, if you're manually spawning a thread on iOS 4+, you're doing it wrong. There are exceptions, but not often.

The first question here should be "why do I even have a background thread for this?"

If you really need a background thread, then the way you're doing segnaread is probably fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文