Objective-C Callbacks/Block Pattern

2019-06-03 15:18发布

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.

1条回答
爷的心禁止访问
2楼-- · 2019-06-03 16:15

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");
}
查看更多
登录 后发表回答