在继续执行下一个命令之前如何让程序等待异步NSURLConnection完成? [英] How to make program wait for asynchronous NSURLConnection to finish before proceeding with next command?

查看:117
本文介绍了在继续执行下一个命令之前如何让程序等待异步NSURLConnection完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让我的程序在转到下一行代码之前等待异步NSURLConnection完成?

How can I make my program wait for an asynchronous NSURLConnection to finish before going to the next line of code?

SetDelegate *sjd= [SetDelegate alloc];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:post delegate:sjd];
[connection start];

这是我开始连接的方式,我处理委托中收到的数据,但我想等为了在继续之前结束连接主要是因为这是在for循环中而且必须为我的数据库中的每个元素运行。

This is how I start the connection and I handle the data received in the delegate but I want to wait for the connection to end before proceeding mainly because this is in a for loop and it has to run for each element in my database.

我需要从手机中放入数据数据库到远程数据库并在数据成功放入手机数据库中的数据后被删除。我正在浏览手机数据库中的每个元素并开始连接,这就是为什么我看不出循环中可以完成下一个操作的原因。对于Objective-c编程我是初学者所以我不确定这是否是正确的方法

I need to put data from the phones database to a remote database and after the data was successfully put in the data in the phones database is deleted. I am going through each element in the phone's database and start a connection that's why I don't see how the next stuff can be done from the loop. I'm a beginner when it comes to objective-c programming so I'm not sure if this is the right way or not to do it

使调用同步是不是一个选项,因为它会阻止程序,我有一个应该显示的进度条。

Making the call synchronous is not an option because it blocks the program and i have a progress bar that should show.

推荐答案

你的问题有点奇怪。你不可能限制这个问题。你不能让一行代码等待进程完成而不阻塞某些东西,在这种情况下,循环运行的是什么线程。

Your question is a bit odd. You have impossibly constrained the issue. You cannot have a line of code "wait" for a process to finish w/o it blocking something, in this case whatever thread the loop is running in.

你可以如果你想使用同步调用,它不会阻止你的应用程序,它只会阻止它执行的线程。在您的示例中,您有一个不断获取远程数据的循环,并且您希望UI在完成之前反映出来。但是你不希望你的UI被阻止。这意味着,你的循环的这个线程必须已经在后台线程上,所以你可以随意在循环中进行同步调用,而不会阻塞你的UI线程。如果循环在UI线程上,您需要更改它以执行您想要的操作。

You can use a synchronous call if you wanted to, it doesn't block your app, it only blocks the thread it is executed on. In your example, you have a loop that is continually getting remote data and you want your UI to reflect that until it is done. But you don't want your UI blocked. That means, this thread with your loop already MUST be on a background thread so you can feel free to do a synchronous call in the loop w/o blocking your UI thread. If the loop is on the UI thread you need to change this to do what you want.

您也可以使用异步连接执行此操作。在这种情况下,您的操作可能会实际完成更快的b / c,您可以同时处理多个请求。如果你这样做,你的循环可以保留在UI线程上,你只需要跟踪所有连接,这样当它们完成时你可以将该状态传递给相关的控制器。您需要iVars来跟踪加载状态,并在加载完成时使用协议或NSNotification进行通信。

You could also do this using an asynchronous connection. In that case, your operation may actual complete faster b/c you can have multiple requests in progress at the same time. If you do it that way, your loop can remain on the UI thread and you just need to track all of the connections so that when they are finished you can communicate that status to the relevant controllers. You'll need iVars to track the loading state and use either a protocol or NSNotification to communicate when loading is done.

编辑:在背景线上添加同步呼叫的示例

ADDED EXAMPLE OF SYNCHRONOUS CALL ON BACKGROUND THREAD

如果你想让循环完全完成只有当所有请求都完成并且没有阻止你的UI线程时这是一个简单的例子:

If you want the loop to finish completely only when all requests are finishes and not block your UI thread here's a simple example:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // post an NSNotification that loading has started
    for (x = 0; x < numberOfRequests; x++) {
        // create the NSURLRequest for this loop iteration
        NSURLResponse *response = nil;
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:&response
                                                         error:&error];
        // do something with the data, response, error, etc
    }
    // post an NSNotification that loading is finished
});

无论什么对象需要显示加载状态,都应该观察并处理您在此处发布的通知。循环将在后台线程上同步并完成所有请求,并且您的UI线程将被解除阻塞并响应。这不是唯一的方法,事实上,我会自己使用异步连接,但这是一个如何获得你想要的简单例子。

Whatever objects need to show loading status should observe and handle the notifications you post here. The loop will churn through and make all your requests synchronously on a background thread and your UI thread will be unblocked and responsive. This isn't the only way to do this, and in fact, I would do it using async connections myself, but this is a simple example of how to get what you want.

这篇关于在继续执行下一个命令之前如何让程序等待异步NSURLConnection完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆