Frage

I have worked with iOS but exactly i am not getting What are the difference between this all methods,

Out of this which syntax should i use to call MehodName ?

 (1) [self MehodName];

 (2) [self performSelector:@selector(MehodName) withObject:nil];

 (3) [self performSelectorInBackground:@selector(MehodName) withObject:nil];

 (4) [self performSelectorOnMainThread:@selector(MehodName) withObject:nil waitUntilDone:YES];

 (5) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
                                         (unsigned long)NULL), ^(void) {
    [self MehodName];
});

  (6) dispatch_sync(dispatch_get_main_queue(), ^{
     [self MehodName];       
});

Please Help and Thanks in advance.

War es hilfreich?

Lösung

All the ways are correct ways depending on your requirement

(1) Executes the method on the same thread on which thread currently your code is executing. (may be background thread or a main thread).

(2) 1 and 2 are pretty same but (2) has advantage that you can dynamically determine which selector to call at runtime.

(3) is same as [NSThread detachNewThread] it creates a new thread (means selector not executed on main thread) generally used for webService image fetching etc.

(4) performs the selector on the main thread (generally used for performing UI changes) if method executed on this thread is too long or heavy it hangs the application for that duration as that method is executed on priority

(5)dispatch async used for doing the task which can be done asynchronously which in your case is using (global queue which are concurrent queues) (not executed on main thread in current case) (like heavy database insertion) background image fetch , those methods which gives you callback later on completion

(6) dispatch sync executes the method synchronously (in your case on main thread) (genrally used for login operations , validation etc.).

Andere Tipps

Essentially #1 and #2 are the same. Use #1 under normal conditions.

Items #3 and #5 are essentially the same. #5 uses GCD and is more flexible. Use these to ensure the code is called on a background thread.

Items #4 and #6 are essentially the same. Again, #6 uses GCD and is more flexible. Use these from code on a background thread to run the method on the main thread. This is usually done when the code to be called is UI related.

All the methods mentioned above their own use cases. We can not say anyone of the way is better than other.

(1) - Method you called will executed in the main thread. Please don't execute long running codes in this methods.

(2) - This will give you flexibility to choose your method name in run time. You are passing method name as SEL object which can be determined in the run time. You can build your valid method names from NSString objects. There is limitations for passing parameters when you are using this type, you can only pass up to 2 parameters to this method call. You can only pass NSObject as parameters.

(3) - Methods will be executed in newly created thread. Since newly created is independent on main thread, you can run long running jobs in this method without affecting UI thread. Since compiler will create on demand threads for executing method, this approach is not advisable for background threading.

(4) - This is same as that of #1 method calling. Methods invocation using this approach will be executed in the main thread. But you can use this method for updating UI elements from the background thread. If you want to execute a method in main thread from where you stand in a background thread, you can use this approach.

(5) - It is GCD approach of method calling in Cocoa. In the #5 appraoch your method will be queued in one of the Global Queue (4 Global Queues for an application). All the jobs in the Global queues will be serially completes its execution. You can use #5 instead of #3 because GCD will handle it threads by itself, developer doesn't look after about the thread handling.

(6) - This is also a GCD approach, for executing methods in the main thread from background GCD thread. If the compiler stands in a background GCD queue you can perform jobs in main thread using this approach.

Hope every thing is clear for you....

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top