objective c - OSX application freezes when try to set the value of TextView? -
i new objective-c.
first please see code:
here property:
@property (atomic) iboutlet nstextview *txtresponse;
and here action:
- (ibaction)sendreq:(id)sender { @synchronized(self) { request *req = [[request alloc] init]; voidcallback callback = ^(nsurlresponse *resp, nsdata *data, nserror *error) { nsstring *val = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; [self.txtresponse setstring:val]; //nslog(val); }; [req seturl:[[nsurl alloc] initwithstring:@"http://foo.bar"]]; [req setcallback:callback]; [req send]; } }
the ui freezes when try use [self.txtresponse setstring:val];
tried code without @synchronized(self)
problem?
in advance!
3rd day working objective-c , found answer :)
i used performselectoronmainthread
, solved problem
- (ibaction)sendreq:(id)sender { request *req = [[request alloc] init]; voidcallback callback = ^(nsurlresponse *resp, nsdata *data, nserror *error) { nsstring *val = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; [self performselectoronmainthread:@selector(setresponsevalue:) withobject:val waituntildone:no]; }; [req seturl:[[nsurl alloc] initwithstring:@"http://programming.com"]]; [req setcallback:callback]; [req send]; } - (void)setresponsevalue:(nsstring *)resp { [self.txtresponse setstring:resp]; //nslog(resp); }
Comments
Post a Comment