ios - What is the correct way to reload data in a UICollectionView? -
if getting information album in users assetslibrary , add picture or remove on while app in background. whats best way reload them.
currently have:
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(assetslibrarydidchange:) name:alassetslibrarychangednotification object:_photolistarray];
to initial notification , calls assetlibrarydidchange method:
which looked this:
- (void)assetslibrarydidchange:(nsnotification *)note { //dict nil reload if(note.userinfo==nil) { [self.coverview reloaddata]; return; } //dict empty no reload if(note.userinfo.count == 0) { nslog(@"empty"); return; } else { [self.coverview reloaddata]; } }
although considering removing if's , else , having reload every time.
what i'm getting to:
there seems gap between when app receives notification , updates. apple's default photos app seems able in around 3-4 seconds while takes mine 10!. there more efficient way this?
i went ahead , put nslog before if's see how many times method being called well.
//library updated - (void)assetslibrarydidchange:(nsnotification *)note { nslog(@"photos updated"); //dict nil reload if(note.userinfo==nil) { dispatch_async(dispatch_get_main_queue(), ^{ [self.coverview reloaddata]; nslog(@"note: %@", note.userinfo); nslog(@"array: %@", _photolistarray); return; }); } //dict empty no reload if(note.userinfo.count == 0) { nslog(@"empty"); return; } else { dispatch_async(dispatch_get_main_queue(), ^{ [self.coverview reloaddata]; nslog(@"note2: %@", note.userinfo); nslog(@"array2: %@", _photolistarray); }); } }
and resulted in following:
2013-08-16 10:41:51.622 app[180:1803] photos updated 2013-08-16 10:41:51.635 app[180:60b] note2: { alassetlibraryupdatedassetskey = "{(\n assets-library://asset/asset.jpg?id=cca17be2-5633-4ffe-a113-f21e37b9882c&ext=jpg,\n assets-library://asset/asset.jpg?id=7b05283e-87a5-4cfe-bdbe-7703e311e0df&ext=jpg,\n assets-library://asset/asset.jpg?id=5b1e736f-e21e-4400-9679-5de8215ac09d&ext=jpg\n)}"; } 2013-08-16 10:41:51.638 app[180:60b] array2: ( "<media: 0x176b3410>", "<media: 0x176b5b00>", "<media: 0x176b5c90>", "<media: 0x176b7590>", "<media: 0x176b47c0>", "<media: 0x176b7e10>", "<media: 0x176b83b0>", "<media: 0x176b8990>", "<media: 0x176b8f40>", "<media: 0x176b9530>", "<media: 0x176ba160>", "<media: 0x176ba960>", "<media: 0x175794d0>", "<media: 0x175503c0>", "<media: 0x17571f80>", "<media: 0x1756dc20>", "<media: 0x17571060>", "<media: 0x1757b0d0>", "<media: 0x1757b8f0>", "<media: 0x1757bee0>", "<media: 0x1757bcf0>", "<media: 0x1757ca80>", "<media: 0x1757cbd0>" ) 2013-08-16 10:41:51.827 app[180:3e07] photos updated 2013-08-16 10:41:51.831 app[180:3e07] empty 2013-08-16 10:41:51.893 app[180:3e07] photos updated 2013-08-16 10:41:51.897 app[180:3e07] empty 2013-08-16 10:41:52.059 app[180:3e07] photos updated 2013-08-16 10:41:52.074 app[180:3e07] empty 2013-08-16 10:41:52.092 app[180:5117] photos updated 2013-08-16 10:41:52.095 app[180:5117] empty
the media objects class using add data , have stored along them. name, ext. nothing changes actual image.
without dispatch:
2013-08-16 11:37:53.246 app[296:3d07] photos updated 2013-08-16 11:37:53.255 app[296:3d07] note2: { alassetlibraryupdatedassetskey = "{(\n assets-library://asset/asset.jpg?id=cca17be2-5633-4ffe-a113-f21e37b9882c&ext=jpg,\n assets-library://asset/asset.png?id=d72a00ce-4198-45be-bcc5-8deb8419a5c8&ext=png,\n assets-library://asset/asset.jpg?id=5b1e736f-e21e-4400-9679-5de8215ac09d&ext=jpg\n)}"; } 2013-08-16 11:37:53.260 app[296:3d07] array2: ( "<media: 0x176271b0>", "<media: 0x17577920>", "<media: 0x17579c50>", "<media: 0x17579eb0>", "<media: 0x17579e00>", "<media: 0x1757a9b0>", "<media: 0x1757af50>", "<media: 0x1757b540>", "<media: 0x1757baf0>", "<media: 0x1757c0e0>", "<media: 0x17656510>", "<media: 0x176570f0>", "<media: 0x176572b0>", "<media: 0x17657840>", "<media: 0x17657e20>", "<media: 0x17658400>", "<media: 0x176589e0>", "<media: 0x17659270>", "<media: 0x17650470>", "<media: 0x17659380>", "<media: 0x176507a0>", "<media: 0x17650ab0>", "<media: 0x17659cb0>", "<media: 0x1765a340>" ) 2013-08-16 11:37:53.404 app[296:4e17] photos updated 2013-08-16 11:37:53.408 app[296:4e17] empty 2013-08-16 11:37:53.450 app[296:3d07] photos updated 2013-08-16 11:37:53.458 app[296:3d07] empty 2013-08-16 11:37:53.466 app[296:4e17] photos updated 2013-08-16 11:37:53.470 app[296:4e17] empty
any ui changing stuff should done on main thread.. try:
- (void)assetslibrarydidchange:(nsnotification *)note { //dict nil reload if(note.userinfo==nil) { dispatch_async(dispatch_get_main_queue(), ^{ [self.coverview reloaddata]; return; } } //dict empty no reload if(note.userinfo.count == 0) { nslog(@"empty"); return; } else { dispatch_async(dispatch_get_main_queue(), ^{ [self.coverview reloaddata]; } } }
Comments
Post a Comment