Best Core Data save strategy (when to save data to disk)

best way to save image in core data
core data external storage
save image to core data swift
save video to core data
save array in core data swift 4
when to use core data
core data binary data
swiftui -- core data

Based on your experience, for an iOS app which uses only one main-thread only NSManagedObjectContext, what is the best way to persist user settings and cached data from server to disk with respect to reliability and performance?

I see next options:

  • save context at the end of every change
  • save context only on app's exit (like in Apple's samples)
  • save context on app's exit, going to background or becoming not active (incoming phone call for example)
  • add timer to save context from time to time if it has any changes
  • call specially prepared delayed save routine which will gather together calls to save context to make sure they don't fire too often

Currently we use the 1st option, so I can say that the reliability of it is very good, the data is saved even after Xcode terminate the app during debug session, but performance may suffer when app become more and more complex, esp. when changes to DB can happen at any moment of the app's flow due to async loading of data from server.

On the other side saving at certain events of the app (exit, going to background etc) will give the best performance, but can you say from your experience that it is enough to make sure that user will not loose the data?

I think you should save often, because it's more reliable (you won't lost data if the app crashes) and you can save free up memory occupied by modified but unused objects. At the same moment you don't want to overwhelm db with your save requests. My suggestion is to expose two methods in interface file and choose which one you want to call depending on the situation you have.

- (void)save {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(save) object:nil];
    [_storage save:nil];
}

- (void)setNeedsSave {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(save) object:nil];
    [self performSelector:@selector(save) withObject:nil afterDelay:1.0];
}

Also, have you considered using second managed object context with private queue? You can set it up as a parent context and save / fetch data in background: http://www.cocoanetics.com/2012/07/multi-context-coredata/

How To Save an Image to Core Data With Swift - Better , context on app's exit, going to background or becoming not active (incoming phone call for example) Hard-core data preservation: The best media and methods for archiving your data Daily backup isn’t archiving. If you want your data to survive the decades, you need to use the right tools.

Saving the context in the UIApplicationDelegate methods applicationDidEnterBackground: and applicationWillTerminate: has always been fine for me. I also save in special circumstances like a big data import or something similar.

Offline data storage in iOS - Shubham Jain, context from time to time if it has any changes. This is a good time to save data to disk. Later on, you’ll see how to save more frequently. Build and run to check that the app still works. There are no functional changes just yet! Creating the Data Model. Phew! Now that the Core Data stack is out of the way, it’s finally time to work on the main part of the app.

According to Apple documentation "It is recommended that you save user data at appropriate points throughout the execution of your app, usually in response to specific actions. For example, save data when the user dismisses a data entry screen. Do not rely on specific app state transitions to save all of your app’s critical data".

Why should I use Core Data for my iPhone app?, Based on your experience, for an iOS app which uses only one main-thread only NSManagedObjectContext, what is the best way to persist user settings and  Saving Images to Core Data using Filesystem. In third and last design we save image identifiers to Core Data and the images themselves to the filesystem. It has a lot in common with Core Data’s external storage feature, except for this time image storage is implemented manually and makes no exceptions for small objects. Here is the new entity

I used to save context on every changes but the performance is really not that good. Currently I'm saving the context on the end of the fetching data function. The performance is like 4 times better. Just make sure you have [context lock] and [context unlock] at the beginning and the end of the function if you plan to call the function async in background threads.

Best Core Data save strategy (when to save data to disk), Core Data has been in iOS and macOS going back as far as anyone can The most straightforward strategy is to save images directly to SQLite table. Accessing Core Data directly is a bad practice, so we implement a thin abstraction layer on top of it: ImageStorage saves and loads images to a disk. Chapter 3, The Core Data Stack: Under the hood, Core Data is made up of many parts working together. In this chapter, you’ll learn about how these parts fit together, and move away from the starter Xcode template to build your own customizable system.

How to Save Images and Videos to Core Data Efficiently, You learn in detail about caching using archives in the “Creating a Data Model The next section explains strategies that can be used for precaching. For a good, in-depth explanation of Core Data, read Core Data: Apple's API for and create your Entities and Relationships; then write methods to save and retrieve data. A guide to caching in ASP.NET Core. This post looks at the various techniques available in ASP.NET Core for caching. We'll look at caching of data, partial pages and full pages at the server and client level and explain when to use each.

iOS 7 Programming Pushing the Limits: Develop Advance Applications , You need to balance both in the best possible way. You will learn in detail about caching using archives later in the "Data Model Cache" section later in this chapter. The next section explains strategies that can be used for precaching. create your Entities and Relationships; then write methods to save and retrieve data. 16 Best Core Data save strategy (when to save data to disk) Jan 20 '13 8 Copying UITableViewCell Mar 17 '11 5 NSUrlRequest: where an app can find the default headers for HTTP request?

iOS 6 Programming Pushing the Limits: Advanced Application , UserDefaults; Keychain; Saving files to disk; Core Data; SQLite these Swift technologies, as well as how to pick the the one that best fits your  As data proliferates at an exponential rate, companies must not only store it. They must approach data management expertly and look to new approaches. Companies that take new and creative approaches to data storage will be able to transform their operations and thrive in the digital economy.

Comments
  • Thanks for the link, although simple solutions proved to be less error prone, until it will be 100% needed we agreed not to use multi-threaded Core Data.