Is there a way to monitor a method in Objective C to see when it is finished? This is an SDK method which I don't have any access to.
-
You're going to need to be more specific. If it's a method you're calling yourself, then immediately after you call it, it's "finished." I'm sure that's not what you mean, though, so which method? Are threads involved? Is this something like CoreAnimation where your call is really just scheduling an event that will happen over time? – StilesCrisis Jun 06 '12 at 21:56
-
It's actually a method called saveEventually on the Parse SDK. It allows me to queue methods in the background to send JSON to their servers. They have callback methods for other methods but not this one (on purpose obviously because of the nature of it). However I'd like to know when it is complete. – Matt Hudson Jun 06 '12 at 21:58
-
Here - http://stackoverflow.com/questions/1085479/override-a-method-via-objc-category-and-call-the-default-implementation – Perception Jun 06 '12 at 21:59
-
What you really want is a way to know when `saveEventually` actually saves. Unfortunately, without knowing what the method really does behind the scenes, that's a difficult question to answer. Asynchronous APIs usually should support some sort of callback on completion... if this one doesn't, I'd recommend asking the authors if one could be added. – StilesCrisis Jun 07 '12 at 00:08
-
You are correct, and I realized that after I asked it. Parse has it in other methods. The reason it's not in this method is because saveEventually is a queue method that doesn't require internet access so it doesn't make a lot of sense, I only need it for debugging. Thanks for your insight. – Matt Hudson Jun 07 '12 at 02:02
4 Answers
Generally speaking, no.
The ability to "monitor" certain kinds of operations does sometimes pop up with some delegate protocols, which may have WillFoo
and DidFoo
types of delegate callbacks.

- 27,575
- 16
- 91
- 128
You are describing a case study of aspect orientated programming (AOP), a alternative and extending concept to object orientated programming.
Objective-C or the underlaying C do not offer this — however there is a the possibility to add parts of it to Objective-C/Cocoa by runtime manipulation, like with method swizzeling.
There are several attempts to teach Objective-C AOP on github, but you will have to test them yourself, I never used one.
- https://github.com/moszi/AOP-in-Objective-C
- https://github.com/tomdalling/AspectObjectiveC
- https://github.com/ndcube/AOP-for-Objective-C
Personally I used plain method swizzeling on NSObject to log its dealloc calls once. It was quite handy, as I only logged it, if the Object was of a certain subclass.
I found a blog post, that shows exactly how to do, what you are asking for by using NSInvocations: Aspect Oriented Programming in Objective-C
The code
NSMutableArray* testArray = (NSMutableArray *)[[AOPProxy alloc] initWithNewInstanceOfClass:[NSMutableArray class]];
[(AOPProxy*)testArray interceptMethodEndForSelector:@selector(removeObjectAtIndex:)
withInterceptorTarget:self
interceptorSelector:@selector( removeInterceptor: )];
will call the method removeInterceptor:
at the end of removeObjectAtIndex:

- 52,040
- 14
- 137
- 178
-
-
Objective-C is so elegant and powerful, after years of coding it professional it still wow!s me. – vikingosegundo Jun 06 '12 at 22:44
Not for use in code, no.
There are profiling technologies that can do this for you, like DTrace (which can be used in Instruments), but that won't help you in your code. You could swizzle the method but you really, really shouldn't do that if you aren't sure what you are doing.

- 56,267
- 18
- 167
- 205
It sounds like the answer is no. A little Googling found this:
Saving Objects Offline
Most save functions execute immediately, and inform your app when the save is complete. If you don't need to know when the save has finished, you can use saveEventually instead. The advantage is that if the user currently doesn't have a network connection, saveEventually will store the update on the device until a network connection is re-established. If your app is closed before the connection is back, Parse will try again the next time the app is opened.
From that text, it sounds like the parse API includes variations on this save method that have a callback.

- 128,072
- 22
- 173
- 272
-
Thanks Duncan. I mentioned that. I'm very familiar with the Parse SDK. – Matt Hudson Jun 07 '12 at 02:58