ios - background request cases a CATransaction error -
i trying download image in background. when trying download it, receive next error:
+[catransaction synchronize] called within transaction
i trying download in cellforrowatindexpath of uitableview. have first encountered such problem. in advance. here code that, located in separate file:
- (id) initwithimageid: (nsstring*) imageid { self = [super init]; if (self) { self.receiveddata = [nsmutabledata data]; self.imageid = imageid; notedpreferences* np = [notedpreferences sharedpreferences]; nsstring* url = [[np apiurl] stringbyappendingformat: @"/thumbs/%@.jpg", imageid]; nsurl* imageurl = [nsurl urlwithstring: url]; nsmutableurlrequest *therequest = (nsmutableurlrequest*)[nsmutableurlrequest requestwithurl:imageurl cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:timeout60]; [self basicauthforrequest:therequest withusername:@"app" andpassword:@"123"]; self.theconnection = [[nsurlconnection alloc] initwithrequest:therequest delegate:self]; cfrunlooprun(); } return self; } - (void)basicauthforrequest:(nsmutableurlrequest *)request withusername:(nsstring *)username andpassword:(nsstring *)password { cfstringref usernameref = (__bridge cfstringref)username; cfstringref passwordref = (__bridge cfstringref)password; cfhttpmessageref authoriztionmessageref = cfhttpmessagecreaterequest(kcfallocatordefault, (__bridge cfstringref)[request httpmethod], (__bridge cfurlref)[request url], kcfhttpversion1_1); cfhttpmessageaddauthentication(authoriztionmessageref, nil, usernameref, passwordref, kcfhttpauthenticationschemebasic, false); cfstringref authorizationstringref = cfhttpmessagecopyheaderfieldvalue(authoriztionmessageref, cfstr("authorization")); [request setvalue:(__bridge nsstring *)authorizationstringref forhttpheaderfield:@"authorization"]; cfrelease(authorizationstringref); cfrelease(authoriztionmessageref); } - (void)connection: (nsurlconnection *) connection didreceivedata: (nsdata *) data { [self.receiveddata appenddata: data]; } - (void)connectiondidfinishloading:(nsurlconnection *)connection { if (self.receiveddata != nil) { uiimage* image = [uiimage imagewithdata: self.receiveddata]; if ([self.delegate respondstoselector:@selector(requesthavefinishedwithimage:imageid:success:)]) [self.delegate requesthavefinishedwithimage:image imageid:self.imageid success: true]; } cfrunloopstop(cfrunloopgetcurrent()); } - (void)connection: (nsurlconnection *) connection didfailwitherror: (nserror *) error { if ([self.delegate respondstoselector:@selector(requesthavefinishedwithimage:imageid:success:)]) [self.delegate requesthavefinishedwithimage:nil imageid:self.imageid success: false]; cfrunloopstop(cfrunloopgetcurrent()); } - (void) dealloc { nslog(@"dealloc of downloadimageobject"); }
Comments
Post a Comment