-1

There is a similar question to mine on the following link but it doesn't quite answer my query.

I am setting a helper class for Facebook (follows the delegation pattern) . An example of one of the class methods would be:

+ (void)openSession:(id)delegate;

This method calls a the Facebook openActiveSessionWithReadPermissions method which expects a completionHandler block. Would it make sense to call the delegate method, say sessionStateChanged in the block as follows?

[delegate sessionStateChanged];

Or is it better to use instance methods for the Facebook helper class and call the delegate using [self.delegate sessionStateChanged] in the completionHandler block.

Community
  • 1
  • 1
Lukas
  • 694
  • 1
  • 9
  • 24

2 Answers2

1

You would be better off with a block parameter rather than a delegate as a parameter if it is just for a single callback.

+ (void)openSession:(void (^)(void))sessionStateChangedBlock

That way you don't have to worry about defining a delegate protocol.

If you want to use a delegate, you will have to define a delegate variable at the class level. You can't use [self.delegate sessionStateChanged] because you are saving the delegate as a class variable. self is only available in an instance of the class.

Fruity Geek
  • 7,351
  • 1
  • 32
  • 41
  • I understand this is a solution but were I to use the delegate pattern rather than the block pattern, which of the ones I mentioned makes more sense? – Lukas Feb 08 '13 at 19:17
  • Edited answer to answer to your question about how to invoke the delegate. – Fruity Geek Feb 08 '13 at 20:41
  • @Fruit-Geek Why was my question down-voted? I know that `self` is only available in an instance class that is not my question. I just need to know whether it is better to use the class method and pass an `(id)delegate`parameter to it (the delegate passed adheres to the FacebookHelperDelegate protocol) and use it like so `[delegate sessionStateChanged]` or to use instance methods and call the protocol method with parameter `(id)delegate` as follows: `[self.delegate = delegate];` [self.delegate sessionStateChanged]` Thanks – Lukas Feb 09 '13 at 15:41
0

I tried both methods i.e. using class and instance methods. Any of them will do, though to follow the proper delegation pattern I believe using instance methods is more appropriate.

Lukas
  • 694
  • 1
  • 9
  • 24