I'm writing an RSS reader app for iOS. There will be a tableView with list of articles in a feed. Each cell of that tableView will contain an image - thumbnail for article. I want it to load the most effective manner:
- The highest priority is for visible thumbnails.
- It would be nice if visible thumbnails could load in a proper order: from top to the bottom.
- Non-visible thumbnails must pre-load too. But with the lowest priority.
- It would be nice if loading order of non-visible thumbnails could be the following: from nearest (both from top and bottom) to visible cells to the most distant.
- Each time tableView scrolls it's cells should update their loading priority based on their visibility and distance to visible area.
I'm a bit stuck what approach to use. Most tricky is (5). It's needed because app is likely to be used in narrow bandwidth conditions. So app must make it's best to utilize low connection speed.
For now I have NSOperationQueue for image loading. maxConcurrentOperationCount is 2. Loading NSOperations are non-concurrent (in Apple's terms), i.e. are performed in separate threads. Each NSOperation is just a synchronous NSData's initWithContentsOfURL. Problem with this approach is that I can't control already executing loading operations. Can't event cancel them!
Ideal would be if I could change priority of ALL (including already executing) loading operations at any time. So I'm thinking about switching to concurrent (in Apple's terms) NSOperations with asynchronous NSURLConnection loading. So that I could cancel operation by looking at isCancelled prop between didReceiveData. I'm planning to use that canceling for operations who's priority was lowered: store data already loaded by operation, cancel it, add with new (lower) priority to the queue and resume download. Hope concurrent (in Apple's terms) operations won't slow UI since loading tasks are not CPU intensive and likely to have a high lag.
开发者_开发技巧Also I'm not sure how to prioritize operations. Since NSOperationQueuePriority have only 5 possible values. I.e. how to maintain an order of loading visible (from top to bottom) and non-visible (from close to far) thumbnails?
Please advise)
精彩评论