Core-Data + AFNetworking + UI Updating (Responsiveness)
Posted
by
Mustafa
on Stack Overflow
See other posts from Stack Overflow
or by Mustafa
Published on 2013-06-29T09:57:40Z
Indexed on
2013/06/29
10:21 UTC
Read the original article
Hit count: 377
Here's the scenario:
I'm writing a DownloadManager
, that allows the user to download, pause, cancel, download all, and pause all. The DownloadManager
is a singleton, and uses AFNetworking
to download files. It has it's own private managed object context, so that user can freely use other parts of the application (by adding, editing, deleting) core-data objects. I have a core-data entity DownloadInfo
that stores the download information i.e. fileURL
, fileSize
, bytesRead
, etc. The DownloadManager
updates the download progress in DownloadInfo
(one for each file).
I have a DownloadManagerViewController
which uses NSFetchedResultsController
to show the download status to the user. This download view controller is using the main managed object context.
Now let's say that I have 20 files in the download queue. And let's say that only 3 concurrent downloads are allowed. The download manager should download the file, and show the download progress.
Problem:
The DownloadInfo
objects are being updated by the DownloadManager
at a very high rate. The DownloadManagerViewController
(responsible for showing the download progress) is updating the list using NSFetchedResultsControllerDelegate
methods. The result is that a lot is happening in the main queue and application has very poor responsiveness.
How can I fix this? How can I make the application responsive, while showing the download progress?
I don't know how else to communicate that the download status between
DownloadManager
andDownloadManagerViewController
. Is there another/ a better way to do this?I don't want to use main managed object context in my
DownloadManager
, for reasons mentioned above. Note, that theDownloadManager
is usingAFNetworking
which is handling the requests asynchronously, but eventually theDownloadInfo
objects are updated in the main thread (as a result of the callback methods). Maybe there's a way to handle the downloads and status update operations in a background thread? but how? How will I communicate between the main thread and the background thread i.e. how will I tell the background thread to queue another file for download?
Thanks.
© Stack Overflow or respective owner