0

Sending message in Objective C is not asynchronous according to answer I got Is sending a message in Objective C actually asynchronous?

so my next question is how to actually send message asynchronously in Objective C (within same application) is it even possible (without resorting to complex stuff like threads) ?

Community
  • 1
  • 1
user310291
  • 36,946
  • 82
  • 271
  • 487
  • Threads actually aren't that complicated! They work great to prevent blocking the UI. I suggest you look for some `NSThread` code examples. You'll love the power of threads! – v1Axvw Apr 15 '12 at 19:43
  • OK maybe it's not complicated but it's overkilled for my context need: just non blocking call :) – user310291 Apr 15 '12 at 20:32
  • `-[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]` with the waitUntilDone flag to `NO` is probably the easiest way to go – v1Axvw Apr 15 '12 at 20:45

3 Answers3

1

Check Apple's "Concurrency Programming Guide" in the doc set. You have options such as threads, operation queues, dispatch queues, and more. It's a big subject.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
1

if you want to do real async method calling using Apple frameworks, just have a look to GCD, and more precisely to dispatch_async.

GCD is available for both iOS and OSX and Apple knows what's coming next, so using this "asycn API" will ensure you to be the less prone to update incompatibility.

good reading ;)

edit: ok, if you really don't want any thread, you can declare the method you are calling as (oneway void). I found that while overriding release.

Here's a SO answer explaining what it does: https://stackoverflow.com/a/5495195/700317

hope this helps.

Community
  • 1
  • 1
teriiehina
  • 4,741
  • 3
  • 41
  • 63
  • same remark as above. Interesting but more complicated for my small need: just non blocking call. – user310291 Apr 15 '12 at 20:31
  • ok, sorry, I didn't get the no-thread thing, but, really, thread can be simple (if used the right way). I updated my answer, hope it will solve your problem. – teriiehina Apr 16 '12 at 14:52
1

Assuming you are trying to get something to happen off the main thread there are numerous ways to handle it. Which one you use depends on what you are doing. Check out these Apple docs to start with: https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/doc/uid/TP40008079 https://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html#//apple_ref/doc/uid/10000057i

theMikeSwan
  • 4,739
  • 2
  • 31
  • 44
  • Thanks. But why async should be associated to concurrent programming ? I just want non-blocking call. – user310291 Apr 15 '12 at 20:30
  • 1
    The ability to make a non blocking call requires concurrency (multiple tasks executing at the same, or nearly the same, time). YOu may only need a very small amount of the information in any of those guides but they will have the information you need for any level of concurrency you need. It sounds like you just need to wrap your call in a `dispatch_async` `dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Whatever call(s) you need to make here });` – theMikeSwan Apr 15 '12 at 21:42