简体   繁体   中英

iOS How do I retrieve and store data from URL?

I am looking for best practices.

In my app, I send a request to my server for the URLs of the images I need to download and some meta data. I then need to store these images on disk. Once they are saved I have to update view. Storing on disk is also necessary for offline view so maybe I'll even have to store the file path to CoreData.

I think I can just use NSURLConnection to retrieve the image urls, then the images and then write them to disk. My concern is what should I do when the app is waiting for all this to occur? How do I know when to update, for example, a table? What if the user moves away and the view unloads? Just looking for advice.

Should I create a separate NSObject class to handle all this?

PS: How would I check for an existing internet connection before I do any of this anyway?

if you are already using CoreData, consider storing the images directly in the CoreData-DB. that depends on the size of the image, of course. For synchronization of the db, this might be helpful: http://www.raywenderlich.com/15916/how-to-synchronize-core-data-with-a-web-service-part-1 . They also use a separate class to handle this. To check the internet connection, apple has a wonderful sample app: https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

EDIT: Oh, and NSURLConnection loads data asynchronously by default, so your app will stay responsive during data loading operations, you can handle the update-vioew-operations in the respective callback methods. See the docs: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/ pay special attention to the difference between sendAsynchronousRequest:queue:completionHandler: and sendSynchronousRequest:returningResponse:error:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM