Cocoa - Return information from NSOperation

后端 未结 3 1188
青春惊慌失措
青春惊慌失措 2021-02-06 10:08

I have an IPhone app that uses webservices to get data from a server. I\'m putting each call to the webservice in a NSOperation subclass so that it can be threaded. My questio

3条回答
  •  爱一瞬间的悲伤
    2021-02-06 10:36

    I've personally done this in two ways. Both worked quite well.

    First Way

    You can have a function that returns the "output", and you call that function after the operation has finished. You can observe the key isFinished on the NSOperation object, or use -[NSOperationQueue waitUntilAllOperationsAreFinished] to determine when the operation is finished.

    Second Way

    You can use a delegate or target/action from the NSOperation. Just make sure that when you call the delegate function, or trigger the target/action, you do so on the main thread by using -[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]

提交回复
热议问题