objective c - Very jerky scrolling NSTableView -
i have nsscrollview contains nstableview, has 3 columns, 1 of has custom view in via tablecellview.
to load images cell via background processes, have subclassed cell using below code. scrolling jerky, wondering if there way optimise this, images not large, 48x48, , being displayed @ 51x51.
i suspect fact fetch request being used each row not efficient, , need find way set nsarray each time view changed current, , use instead. wanting efficient possible first.
- (nsview *)tableview:(nstableview *)tableview viewfortablecolumn:(nstablecolumn *)tablecolumn row:(nsinteger)row { // new viewcell nstablecellview *cellview = [tableview makeviewwithidentifier:tablecolumn.identifier owner:self]; //identify correct column if( [tablecolumn.identifier isequaltostring:@"userlogo"] ) { nsfetchrequest *request = [[nsfetchrequest alloc] init]; //set predicate , filter new tweets page if ([self.currenttwitterview isequaltostring:@"new"]) { nspredicate *testfortrue = [nspredicate predicatewithformat:@"(approved == no) , (tweetdeleted == no) , (scheduledtweet == no)"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"postdate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , predicate approved tweets page } else if ([self.currenttwitterview isequaltostring:@"approved"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"(approved == yes) , (tweetdeleted == no) , (scheduledtweet == no)"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"approveddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , preicate deleted tweets page } else if ([self.currenttwitterview isequaltostring:@"deleted"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"tweetdeleted == yes"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"deleteddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , preicate deleted tweets page } else if ([self.currenttwitterview isequaltostring:@"scheduled"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"scheduledtweet == yes"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"scheduleddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; } //setup request [request setentity:[nsentitydescription entityforname:@"tweet" inmanagedobjectcontext:_managedobjectcontext]]; //assign predicate fetch request nserror *error = nil; //create array returned objects nsarray *fetchedobjects = [_managedobjectcontext executefetchrequest:request error:&error]; tweet *selectedtweet = [fetchedobjects objectatindex:row]; cellview.imageview.image = nil; dispatch_async(dispatch_queue_create("getasynchroniconsgdqueue", null), ^{ nsurl *url = [nsurl urlwithstring:selectedtweet.avatarurl]; nsimage *image = [[nsimage alloc] initwithcontentsofurl:url]; cellview.imageview.image = image; }); [cellview setwantslayer:yes]; return cellview; } [cellview setwantslayer:yes]; return cellview; }
thanks
gareth
edit 1
ok have tried implementing afimagerequest, , performance worst, , seem getting multiple copies of same image/the wrong image in various rows.
here code using.
@synthesize profileimage = _profileimage; + (nsoperationqueue *)sharedprofileimagerequestoperationqueue { static nsoperationqueue *_sharedprofileimagerequestoperationqueue = nil; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ _sharedprofileimagerequestoperationqueue = [[nsoperationqueue alloc] init]; [_sharedprofileimagerequestoperationqueue setmaxconcurrentoperationcount:8]; }); return _sharedprofileimagerequestoperationqueue; } //load image table - (nsview *)tableview:(nstableview *)tableview viewfortablecolumn:(nstablecolumn *)tablecolumn row:(nsinteger)row { // new viewcell nstablecellview *cellview = [tableview makeviewwithidentifier:tablecolumn.identifier owner:self]; //identify correct column if( [tablecolumn.identifier isequaltostring:@"userlogo"] ) { nsfetchrequest *request = [[nsfetchrequest alloc] init]; //set predicate , filter new tweets page if ([self.currenttwitterview isequaltostring:@"new"]) { nspredicate *testfortrue = [nspredicate predicatewithformat:@"(approved == no) , (tweetdeleted == no) , (scheduledtweet == no)"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"postdate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , predicate approved tweets page } else if ([self.currenttwitterview isequaltostring:@"approved"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"(approved == yes) , (tweetdeleted == no) , (scheduledtweet == no)"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"approveddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , preicate deleted tweets page } else if ([self.currenttwitterview isequaltostring:@"deleted"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"tweetdeleted == yes"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"deleteddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , preicate deleted tweets page } else if ([self.currenttwitterview isequaltostring:@"scheduled"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"scheduledtweet == yes"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"scheduleddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; } //setup request [request setentity:[nsentitydescription entityforname:@"tweet" inmanagedobjectcontext:_managedobjectcontext]]; //assign predicate fetch request nserror *error = nil; //create array returned objects nsarray *fetchedobjects = [_managedobjectcontext executefetchrequest:request error:&error]; tweet *selectedtweet = [fetchedobjects objectatindex:row]; nsurl *url = [nsurl urlwithstring:selectedtweet.avatarurl]; /*cellview.imageview.image = nil; dispatch_async(dispatch_queue_create("getasynchroniconsgdqueue", null), ^{ nsurl *url = [nsurl urlwithstring:selectedtweet.avatarurl]; nsimage *image = [[nsimage alloc] initwithcontentsofurl:url]; cellview.imageview.image = image; }); */ _avatarimagerequestoperation = [afimagerequestoperation imagerequestoperationwithrequest:[nsurlrequest requestwithurl:url] success:^(nsimage *image) { cellview.imageview.image = self.profileimage; _avatarimagerequestoperation = nil; [[nsnotificationcenter defaultcenter] postnotificationname:kuserprofileimagedidloadnotification object:self userinfo:nil]; }]; [_avatarimagerequestoperation setcacheresponseblock:^nscachedurlresponse *(nsurlconnection *connection, nscachedurlresponse *cachedresponse) { return [[nscachedurlresponse alloc] initwithresponse:cachedresponse.response data:cachedresponse.data userinfo:cachedresponse.userinfo storagepolicy:nsurlcachestorageallowed]; }]; [[[self class] sharedprofileimagerequestoperationqueue] addoperation:_avatarimagerequestoperation]; //cellview.imageview.image = self.profileimage; //[cellview setwantslayer:yes]; return cellview; }
a few things spring mind:
- currently, you're thrashing network - there's no caching, , each time cell loaded, perform networking retrieve file has
- have tried afnetworking image retrieval? there's particularly pleasant 'uiimageview+afnetworking.h' category, need, well.
- have tried moving following off of main thread?
nsfetchrequest *request = [[nsfetchrequest alloc] init];
//set predicate , filter new tweets page if ([self.currenttwitterview isequaltostring:@"new"]) { nspredicate *testfortrue = [nspredicate predicatewithformat:@"(approved == no) , (tweetdeleted == no) , (scheduledtweet == no)"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"postdate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , predicate approved tweets page } else if ([self.currenttwitterview isequaltostring:@"approved"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"(approved == yes) , (tweetdeleted == no) , (scheduledtweet == no)"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"approveddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , preicate deleted tweets page } else if ([self.currenttwitterview isequaltostring:@"deleted"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"tweetdeleted == yes"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"deleteddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; //set filter , preicate deleted tweets page } else if ([self.currenttwitterview isequaltostring:@"scheduled"]){ nspredicate *testfortrue = [nspredicate predicatewithformat:@"scheduledtweet == yes"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"scheduleddate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; } //setup request [request setentity:[nsentitydescription entityforname:@"tweet" inmanagedobjectcontext:_managedobjectcontext]]; //assign predicate fetch request nserror *error = nil; //create array returned objects nsarray *fetchedobjects = [_managedobjectcontext executefetchrequest:request error:&error]; tweet *selectedtweet = [fetchedobjects objectatindex:row];
Comments
Post a Comment