IOS objective C write function with completionBlock -


hi i'm writing function call , api , response store coredata entity (using magical record). method simple take no input , return nothing (only call api , store data entity. purpose execute completionblock code when success , log error (so can call function in viewcontroller. i'm still don't know how write properly. anyhelp appreciate.

my apiclient

    typedef void (^apiclientsuccess)(id responseobject);     typedef void (^apiclientfailure)(nserror *error);   - (void)getsingpostcontentsonsuccess:(apiclientsuccess)success onfailure:(apiclientfailure)failure {      nsstring *urlstring = [nsstring stringwithformat:@"%@singpost-contents.php",cms_base_url];     nsmutableurlrequest *request = [[afhttprequestserializer serializer] requestwithmethod:get_method urlstring:urlstring parameters:nil error:nil];      [self sendjsonrequest:request success:^(nsurlresponse *response, id responseobject) {         success(responseobject);     } failure:^(nserror *error) {         failure(error);         [self reportapiissueurl:[request.url absolutestring] payload:nil message:[error description]];     }]; } 

my function (need write properly) can call , write code when function complete execute

+ (void)api_singpostcontentoncompletion:(void)completionblock {      [[apiclient sharedinstance] getsingpostcontentsonsuccess:^(id responsejson) {         dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_default, 0), ^{             nsmanagedobjectcontext *localcontext = [nsmanagedobjectcontext mr_rootsavingcontext];             [responsejson[@"root"] enumerateobjectsusingblock:^(id attributes, nsuinteger idx, bool *stop) {                 content *content = [content mr_findfirstorcreatebyattribute:@"name" withvalue:attributes[@"name"] incontext:localcontext];                 content.content = attributes[@"content"];             }];              [localcontext mr_savetopersistentstorewithcompletion:^(bool contextdidsave, nserror * _nullable error) {                 if (error) nslog(@"error save content persistentstore");              }];  //            if (completionblock) { //                dispatch_async(dispatch_get_main_queue(), ^{ //                    completionblock(); //                }); //            }         });     } onfailure:^(nserror *error) { //        if (completionblock) { //            dispatch_async(dispatch_get_main_queue(), ^{ //                completionblock(nil); //            }); //        }         nslog(@"failed singpost content");     }];  } 

declare blocks :

  typedef void (^ successblock)(bool success, id response);   typedef void (^ failureblock)(nserror *error, nsinteger statuscode); 

write method use these blocks, example simple afnetworking post call:

+ (void)myfunction:(nsstring*)function  success:(successblock)success failure:(failureblock)failure{     afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager];         ....    [manager post:functionurl parameters:parameters success:^(afhttprequestoperation *operation, id responseobject) {             if (operation.response.statuscode == 201 || operation.response.statuscode == 200) {                 success(yes,responseobject);             }else{                  success(no,responseobject);             }         } failure:^(afhttprequestoperation *operation, nserror *error) {             failure(error, operation.response.statuscode);         }];   }  

and can call method other class:

[yourclass myfunction:@""  success:^(bool success, id response) {        if(success){             //my call success        }     } failure:^(nserror *error, nsinteger statuscode) {      }]; 

Comments