Sam
Sam

Reputation: 15770

Objective-C Callbacks/Block Pattern

What I am trying to do is load a list of people (JSON format) from a remote server, save the file onto disk, and then parse the result and return an NSArray * back to the caller.

I have created a EmployeeDirectoryManager that has the following:

- (NSArray *)loadDirectory:(BOOL)refreshFromServer;
- (void)loadDirectoryFromFile;
- (void)loadDirectoryFromServer;

I would like to use a block on the loadDirectory method so the caller can be informed when the loadDirectoryFromServer, which is using an AFJSONRequestOperation which has a success block on it.

I need a little direction on how to implement this, or if I am headed down the wrong path.

Upvotes: 1

Views: 1009

Answers (1)

Philip J. Fry
Philip J. Fry

Reputation: 1165

To use block in your methods as completion handler, firstly you need define new type

typedef void(^TypeComplitionHandler)(id result)

Then you can pass block to your method. For example

- (void)loadDirectoryFromFileComplitionHandler:(TypeComplitionHandler)complition {
    complition(@"done");
}

Upvotes: 1

Related Questions