简体   繁体   中英

converting objective-c block-oriented api to android

I've been tasked with converting our webservice layer in iOS, which is entirely written with blocks, into something similar for Android. As Java doesn't have closures yet, what would be a reasonable approach?

Example api call:

if (![data.locationsAPI cityNamesForCity:self.searchBox.text
                              onSuccess:^(JSONRef * ref)
     {
         locations = [[ref valueForKey:@"cities"]arrayCopy];
         [self.searchTable reloadData];
         [self.hotelTable reloadData];

     } onFailure:^(IOError * error)
     {
         locations = nil;
         [self.searchTable reloadData];
     }])
{
    [self notReachableAlert];
}

The success/failure blocks are called on the main thread while the api call internally is in a background thread.

I could do something similar with an anonymous class which would be passed the original class which could then dispatch to some method to process the result. Is there a better way?

You could use pattern Delegate http://en.wikipedia.org/wiki/Delegation_pattern . Pass the delegate object which adopt some of the methods. And after getting some result call this methods with result values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM