1

I have the following scenario:

Class_A
  - method_U
  - method_V
  - method_X
  - method_Y

Class_B
  - method_M
  - method_N

HttpClass
  - startRequest
  - didReceiveResponse // is a callback

Now I want to realize these three flows (actually there are many more, but these are enough to demonstrate my question):

Class_A :: method_X -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_A :: method_Y:result

and:

Class_A :: method_U -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_A :: method_V:result

and the last one:

Class_B :: method_M -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_B :: method_N:result

Please note, that the methods in Class_A and Class_B have different names and functionality, they just make us of the same HttpClass.

My solution now would be to pass a C function pointer to startRequest, store it in the HttpClass and when didReceiveResponse gets called I invoke the function pointer and pass the result (which will always be a JSON Dictionary).

Now I'm wondering if there can be any problems using plain C or if there are better solutions doing it in a more Objective-C way. Any ideas?

2 Answers 2

4

You can use selectors instead of function pointers. Basically you setup for 1st scenario would be:

// declare startRequestWithDelegate method
- startRequestWithDelegate:(id)delegate selector:(SEL)selector;

// call it in Class_A object:
[HTTPObject startRequestWithDelegate:self selector:@selector(method_Y:);

// call callback method when request finished:
if ([delegate respondsToSelector:savedSelector])
   [delegate performSelector:savedSelector withObject:result];

Other scenarios will differ in calling startRequestWithDelegate method.

Sign up to request clarification or add additional context in comments.

Comments

1

I can think of several ways that you could do this, each of which has some benefits (and drawbacks):

  • Have HttpClass post notifications in didReceiveResponse for which Class_A and Class_B could listen
  • Pass Class_A and Class_B as delegates to HttpClass, which would be called in didReceiveResponse
  • Use a target and action callback (very similar to the delegate approach)

Personally I would use delegates- it would make it simpler to add more functionality in the future and is more robust than using straight function pointers.

1 Comment

Not sure how the first example would work, since I have various methods in the same class which need to be called back. The second solution is the same Vladimir suggested and that's what I implemented now. Works perfectly :-) The last one I read about here: developer.apple.com/library/archive/documentation/Cocoa/… but wasn't sure if this is only for UI elements. But as you said it's very similar to the delegat/selector approach. Thanks for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.