JSONCache

public struct JSONCache

JSONCache is a thin layer on top of Core Data that seamlessly consumes, caches and produces JSON data.

  • Automatically creates Core Data objects from JSON data, or merges JSON data into objects that already exist.
  • Automatically maps 1:1 and 1:N relationships based on inferred knowledge of your Core Data model.
  • If necessary, automatically maps between snake_case in JSON and camelCase in Core Data attribute names.
  • Generates JSON on demand, both from NSManagedObject instances, and from any struct that adopts the JSONifiable protocol.
  • Operates on background threads to avoid interfering with your app’s responsiveness.
  • Enum defining the JSON casing conventions supported by JSONCache.

    See more

    Declaration

    Swift

    public enum Casing
  • Enum defining the JSON date formats supported by JSONCache

    See more

    Declaration

    Swift

    public enum DateFormat
  • Defines the casing convention used in consumed and/or produced JSON data.

    Declaration

    Swift

    public static var casing: JSONCache.Casing
  • Defines the date format used in consumed and/or produced JSON data.

    Declaration

    Swift

    public static var dateFormat: JSONCache.DateFormat
  • The managed object context associated with the main queue.

    Declaration

    Swift

    public static var mainContext: NSManagedObjectContext!
  • Boostrap the Core Data stack. Must be called before invoking any other methods.

    Declaration

    Swift

    public static func bootstrap(withModelName modelName: String, inMemory: Bool = false, bundle: Bundle = .main, completion: @escaping (_ result: Result<Void, JSONCacheError>) -> Void)

    Parameters

    modelName

    The name of the managed object model to use.

    inMemory

    true if the persistent store should be in memory only. Defaults to false if not given, in which case the persistent store is of SQLLite type.

    bundle

    The Bundle instance to use to look up the managed object model. Defaults to Bundle.main if not given.

    completion

    A closure to be executed when bootstrapping is complete.

    result

    A Result<Void, JSONCacheError> instance passed to the completion closure that should be inspected to determine if bootstrapping completed successfully.

  • Boostrap the Core Data stack and return a ResultPromise<Void, JSONCacheError> instance that you should await() to obtain the result. Must be called before invoking any other JSONCache methods that interact with Core Data.

    Declaration

    Swift

    public static func bootstrap(withModelName modelName: String, inMemory: Bool = false, bundle: Bundle = .main) -> ResultPromise<Void, JSONCacheError>

    Parameters

    modelName

    The name of the managed object model to use.

    inMemory

    true if the persistent store should be in memory only. Defaults to false if not given, in which case the persistent store is of SQLLite type.

    bundle

    The Bundle instance to use to look up the managed object model. Defaults to Bundle.main if not given.

    Return Value

    A ResultPromise<Void, JSONCacheError> instance that when fulfilled will contain a Result<Void, JSONCacheError> instance describing the result.

  • Stage a JSON dictionary for loading into Core Data.

    Declaration

    Swift

    public static func stageChanges(withDictionary dictionary: [String : Any], forEntityWithName entityName: String)

    Parameters

    dictionary

    The JSON dictionary to load into Core Data.

    entityName

    The name of the Core Data entity that will hold the dictionary data.

  • Stage an array JSON dictionaries for loading into Core Data.

    Declaration

    Swift

    public static func stageChanges(withDictionaries dictionaries: [[String : Any]], forEntityWithName entityName: String)

    Parameters

    dictionaries

    The array of JSON dictionaries to load into Core Data.

    entityName

    The name of the Core Data entity that will hold the dictionary data.

  • Load the staged JSON dictionaries into Core Data on a background thread.

    Declaration

    Swift

    public static func applyChanges(completion: @escaping (_ result: Result<Void, JSONCacheError>) -> Void)

    Parameters

    completion

    A closure to be executed when loading has completed.

    result

    A Result<Void, JSONCacheError> instance passed to the completion closure that should be inspected to determine if the dictionaries were successfully loaded into Core Data.

  • Load the staged JSON dictionaries into Core Data on a background thread, and return a ResultPromise<Void, JSONCacheError> instance that you should await() to obtain the result.

    Declaration

    Swift

    public static func applyChanges() -> ResultPromise<Void, JSONCacheError>

    Return Value

    A ResultPromise<Void, JSONCacheError> instance that when fulfilled will contain a Result<Void, JSONCacheError> instance describing the result.

  • Save any changes in the parent store of the given managed object context. Roll back the save operation if an error occurs.

    Declaration

    Swift

    public static func save(context: NSManagedObjectContext = mainContext) -> Result<Void, JSONCacheError>

    Parameters

    context

    The managed object context whose parent store should be saved. Defaults to mainContext if not given.

    Return Value

    A Result<Void, JSONCacheError> instance that should be inspected to determine if the save operation completed successfully.

  • Fetch a managed object from the persistent store.

    Declaration

    Swift

    public static func fetchObject<T>(ofType entityName: String, withId identifier: AnyHashable, in context: NSManagedObjectContext? = mainContext) -> Result<T?, JSONCacheError> where T : NSManagedObject

    Parameters

    entityName

    The entity name of the object to be fetched.

    identifier

    The primary key value of the object. The entity’s primary key is either an attribute with name id, or an attribute with the User Info key JC.isIdentifier set to true or YES.

    context

    The managed object context from which to fetch the object. Defaults to mainContext if not given.

    Return Value

    A Result<NSManagedObject?, JSONCacheError> instance from which the fetched object can be retrieved if the fetch operation completed successfully.

  • Fetch an array of managed objects from the persistent store.

    Declaration

    Swift

    public static func fetchObjects<T>(ofType entityName: String, withIds identifiers: [AnyHashable], in context: NSManagedObjectContext? = mainContext) -> Result<[T], JSONCacheError> where T : NSManagedObject

    Parameters

    entityName

    The entity name of the objects to be fetched.

    identifiers

    An array containing the primary key values of the objects. The entity’s primary key is either an attribute with name id, or an attribute with the User Info key JC.isIdentifier set to true or YES.

    context

    The managed object context from which to fetch the objects. Defaults to mainContext if not given.

    Return Value

    A Result<[NSManagedObject], JSONCacheError> instance from which the fetched objects can be retrieved if the fetch operation completed successfully.