iphone - Making http request in for loop fails in ios -
i doing lots of web request asynchronous call in loop 3 call success out of 10 call. when made synchronous success rate 100%. want asynchronously.please me. sample code following:
+(void)saveexpenceinbackground { nsarray *array = [expense getsortedexpenseforuser:[coredatamanager sharedmanager].user forcontext:[coredatamanager sharedmanager].managedobjectcontext]; (expense *expence in array) { nsdictionary *reqdict = [kzexpencedataandservicehandler convertobjecttodictionary:expence]; [expencedataandservicehandler expencesavetoserver:reqdict]; } } +(void) expencesavetoserver: requestdict :(nsdictionary *)reqdict { @try { nsdata * jsondata = [nsjsonserialization datawithjsonobject:reqdict options:0 error:&error]; nsstring *datastring = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; nsstring *str = [nsstring stringwithformat:@"method=%@&data=%@",[postdict valueforkey:@"method"], datastring]; nsstring *webmethod = [self httpmethodtypeforservicetype:method]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:[nsurl urlwithstring:[[userpreference objectforkey:kzbaseurl] stringbyappendingstring:relativepath]]]; [request sethttpmethod:webmethod]; // [r setallhttpheaderfields:dictheaders]; [request sethttpbody:[str datausingencoding:nsutf8stringencoding]];//[inputparameter.dict_postparameters objectforkey:webservicejsondatapostdictkey] [request settimeoutinterval:120]; [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) { if (connectionerror) { return; } nsdictionary *responsedict = [btcwebservicehelper jsonvalidationwithdictionary:data]; if (responsedict != nil) { nsmutabledictionary *dic = [responsedict mutablecopy]; [dic setobject:[reqdict valueforkey:@"expence_id"] forkey:@"expence_id"]; } }]; } @catch (nsexception *exception) { nslog(@"exception %@",exception); } @finally { } } +(nsdictionary *)convertobjecttodictionary:(expense *)expence { nsdictionary *requestdict = [@{@"receipt_key":expence.receipt_key, @"store_name": expence.store_name, @"amount": expence.amount, @"date_purchase": expence.date_purchase, @"is_processed":expence.is_processed?[nsnumber numberwithbool:no]:[nsnumber numberwithbool:yes], @"item_name":expence.item_name , @"user_folder": expence.user_folder , @"folder_name": expence.folder_name, @"category_id": expence.category_id, @"is_deleted": expence.is_deleted?[nsnumber numberwithbool:no]:[nsnumber numberwithbool:yes], @"is_approved": expence.is_approved?[nsnumber numberwithbool:no]:[nsnumber numberwithbool:yes], @"active_vat_percentage":![[nsstring stringwithformat:@"%@",expence.active_vat_percentage] isequal: @"nan"]?expence.active_vat_percentage:@"" , @"vat_qualified": expence.vat_qualified?[nsnumber numberwithbool:no]:[nsnumber numberwithbool:yes], @"vat_percentage": ![[nsstring stringwithformat:@"%@",expence.vat_percentage] isequal: @"nan"]?expence.vat_percentage:@"", @"vat_amount": ![[nsstring stringwithformat:@"%@",expence.vat_amount] isequal: @"nan"]?expence.vat_amount:@"", @"net_expense": ![[nsstring stringwithformat:@"%@",expence.net_expense] isequal: @"nan"]?expence.net_expense:@"", @"is_fuel": expence.is_fuel?[nsnumber numberwithbool:no]:[nsnumber numberwithbool:yes], @"fuel_type": ![[nsstring stringwithformat:@"%@",expence.fuel_type] isequal: @"nan"]?expence.fuel_type:@"", @"fuel_quantity": ![[nsstring stringwithformat:@"%@",expence.fuel_quantity] isequal: @"nan"]?expence.fuel_quantity:@"", @"fuel_amount": ![[nsstring stringwithformat:@"%@",expence.fuel_amount] isequal: @"nan"]?expence.fuel_amount:@"", @"fuel_rate": ![[nsstring stringwithformat:@"%@",expence.fuel_rate] isequal: @"nan"]?expence.fuel_rate:@"", @"bank_account_id": ![[nsstring stringwithformat:@"%@",expence.bank_account_id] isequal: @"nan"]?expence.bank_account_id:@"", @"is_paid": expence.is_paid?[nsnumber numberwithbool:no]:[nsnumber numberwithbool:yes], @"project_id": ![[nsstring stringwithformat:@"%@",expence.project_id] isequal: @"nan"]?expence.project_id:@"", @"description": @"", @"payment_method": ![[nsstring stringwithformat:@"%@",expence.payment_method] isequal: @"nan"]?expence.payment_method:@"", @"currency_id": expence.currency_id, @"image_name": [nsstring stringwithformat:@"img%@",expence.receipt_key], @"receipt_format": expence.receipt_format?expence.receipt_format:@"", @"image_original": expence.image?expence.image:@"", @"is_synced": expence.is_synced?[nsnumber numberwithbool:no]:[nsnumber numberwithbool:yes], @"expence_id":expence.expence_id?expence.expence_id:@"0", @"receipt_uid":expence.receipt_uid?expence.receipt_uid:@"", @"image_name":expence.image_name?expence.image_name:@"" } mutablecopy]; return requestdict; }
Comments
Post a Comment