Master data, file uploads and thread safety

What's the preferred approach for streaming persistent data when using Core Data? I am uploading a large file and want to show the upload progress in UIProgressBar

. The actual download happens on a background thread created NSOperation

.

The download information (local path, shared bytes, received bytes) is modeled as an underlying data-driven object and the actual file is stored in the Documents / directory. One of the solutions that came to my mind was to create a separate managed object context on a background thread and pass it objectID

and pull it using the method objectWithID:

. Whenever a background thread performs a save, the main thread is notified and the main context merges those changes and the table view is subsequently updated.

This approach works, but the save cannot be done too often, or the user interface freezes. This way, the UI is refreshed after every X KB of data is received, where X must be at least 500 KB for the UI to be somewhat responsive. Is there a better way to pass download progress data to the main thread as it is received?

EDIT: Would using KVO be of any help? If so, do you know of any good tutorials on the topic?

+2


source to share


2 answers


I know you have already created your own system, but I use ASIHTTPRequest for all my network operations. It is very reliable and has a ton of useful features like resuming files, saving directly to disk, monitoring download progress, monitoring progress tracking, and a kitchen sink. If you are not using it, you can look at the source to see how they do it, because the UI never hangs when I use the progress report on this framework.



+4


source


While I'm going to use ASIHTTPRequest for my project, it's still good to mention my solution to the problem for completeness. It's obvious, but saving the main information context as often as every couple of seconds is a terrible mistake.

Instead, I added a progress delegate to the upload operation, which receives an update notification on the main thread.



NSNumber bytesDownloaded = [NSNumber numberWithLongLong:[data length]];
[downloadDelegate performSelectorOnMainThread:@selector(updateProgress:) withObject:bytesDownloaded waitUntilDone:NO];

      

It was important to pass information about the download progress to the delegate in the main thread. The delegate updates the progress, saves the accumulated changes, and saves it when the download completes or occurs at much larger intervals.

0


source







All Articles