我正在开发一款应用程序,允许用户从 iPad 在云上创建文件和文件夹。
删除文件后,应用程序会自动在云上创建一个“回收站”文件夹并将该文件放入其中。我已经为所有操作创建了 NSOperationQueue ,即我有一个用于文件夹创建的单独队列和一个用于文件上传的单独队列。我面临的问题是,文件上传操作在文件夹创建操作完成之前执行,因此文件无法成功上传。
谁能帮助我使文件夹创建操作同步?
我已尝试以下代码
[create_folder_queue addOperations:[NSArray arrayWithObject:folderOperation] waitUntilFinished:YES];
,但它不执行该操作。
提前致谢。
I am working on an app that allows an user to create files and folders on the cloud from iPad.
When a file is deleted, the app automatically creates a 'Recycle Bin' folder on the cloud and puts that file in it. I have created NSOperationQueue
for all the operations i.e. I have separate queue for Folder Creation and a separate queue for File Upload. The problem that I am facing is that, the operation for file upload gets executed before the operation for folder creation is completed and so file doesn't gets uploaded successfully.
Can anyone help me to make the folder creation operation synchronous?
I have tried the following code
[create_folder_queue addOperations:[NSArray arrayWithObject:folderOperation] waitUntilFinished:YES];
but it doesn't execute the operation.
Thanks in advance.
发布评论
评论(6)
如果您想要同步队列,请运行
-setMaxConcurrentOperationCount:
操作队列上的方法将其设置为<代码>1。设置此属性后,队列中一次最多只能运行一个操作,这或多或少是同步队列的定义,其中一个任务仅在另一任务完成后执行。
If you want a synchronous queue, run the
-setMaxConcurrentOperationCount:
method on your operation queue to set it to1
.After setting this property, you will only have at most one operation running at a time in the queue, which is more or less the definition of a synchronous queue, where one task only follows after another is complete.
据我所知,一个 NSOperation 对象可以依赖于其他对象,并且并不强制它们在同一操作队列中执行。所以你可以做下一步:
注意这个例子中没有内存管理。
As far as I know one
NSOperation
object can depend on other and it is not mandatory for them to be executed in the same operation queue. So you can do next:Pay attention that there is no memory management in this example.
您可能不应该使用单独的操作队列。由于这些都是云文件操作,因此您应该有一个云文件操作队列来执行所有云文件操作。
如果这些操作不依赖于其他操作,您只需将它们添加到此队列中,它们就会以任何顺序执行,并且可以同时执行系统认为有用的任意数量的操作。当然,如果您认为这是个好主意,您可以设置 maxConcurrentOperationCount 来对此设置自己的限制,但您不必这样做,在这种情况下系统会为您决定。
如果操作确实相互依赖,则可以使用操作依赖系统。因此,您首先创建操作,然后使用 addDependency 告诉系统哪个操作依赖于哪个其他操作。如果您现在将所有这些操作添加到队列中,系统将确保以正确的顺序运行它们,以便操作仅在其依赖的所有操作完成后才会运行(并且这些操作可能再次依赖于其他操作,因此在)。只需确保不创建循环依赖项(OpA 依赖于 OpB,OpB 又依赖于 OpC,而 OpC 又依赖于 OpA),否则它们都不会运行。
但对于一般问题的答案,如果其他用户询问如何使用
NSOperationQueue
模拟dispatch_sync
:但是请注意,这远低于调用
的性能在串行调度队列上调用dispatch_sync
,因为上面的代码总是导致两个线程切换,而在串行调度队列上调用dispatch_sync将重用调用线程来运行块,因此根本没有线程切换将会发生,也不该进程是否需要第二个线程来执行此操作。 请参阅此处了解其工作原理的详细信息。You probably shouldn't use separate operation queues. As these are all cloud file operations, you should have one cloud file operation queues performing all cloud file operations.
If the operations don't depend on other operations, you just add them to this queue and they are performed in any order and as many of them at the same time as the system considers useful. Of course, you can set
maxConcurrentOperationCount
to place your own limit on this if you think that is a good idea, but you don't have to, in which case the system will decide for you.In case operations do depend on each other, you use the operation dependency system. Therefor you first create the operations and then use
addDependency
to tell the system which operation depends on which other operation. If you now add all these operations to the queue, the system will make sure to run them in the correct order, so that an operation will only run after all operations it depends on have finished (and these may again depend on other operations and so on). Just make sure to not created cyclic dependencies (OpA depends on OpB which depends on OpC which depends on OpA) as then none of them will ever run.But to the answer the question in general, in case other users ask how you can simulate a
dispatch_sync
withNSOperationQueue
:Note however, that this far less performant than calling
dispatch_sync
on a serial dispatch queue since the code above always causes two thread switches whereas callingdispatch_sync
on a serial dispatch queue will reuse the calling thread to run the block and thus no thread switch at all will take place, nor will the process require a second thread to do so. See here for details on how this works.这应该可以解决您的问题:
This should solve your issue:
与
setMaxConcurrentOperationCount
一起,在您的NSOperation
子类中:
现在处于完全串行序列模式。
Along with
setMaxConcurrentOperationCount
, in yourNSOperation
subclass:
Now it's in a completely serial sequence mode.
为什么你有两个队列?如果您只能拥有一个,那么您的问题可以通过依赖项解决(请参阅 文档)。
Why do you have two queues? If you can have only one, then your problem is solved with dependencies (see the documentation).