Core Data setup in iOS app

NOVASOLUTIONS.TECHNOLOGY is engaged in the development, support and maintenance of iOS, Android, PWA mobile applications. We have extensive experience and expertise in publishing mobile applications in popular markets like Google Play, App Store, Amazon, AppGallery and others.
Development and support of all types of mobile applications:
Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Showing 1 of 1 servicesAll 1735 services
Core Data setup in iOS app
Medium
~2-3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    756
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    624
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1052
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    947
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    862
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    445

Core Data Setup in iOS Applications

Core Data is not just SQLite wrapper. It's object graph with lazy loading, caching, change tracking, and CloudKit sync capability. Properly configured it speeds up local data work. Improperly — causes deadlocks and crashes on NSFetchedResultsController.

Stack Configuration

Starting iOS 10, recommended way is NSPersistentContainer. Encapsulates NSManagedObjectModel, NSPersistentStoreCoordinator, and main NSManagedObjectContext.

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "DataModel")
    container.loadPersistentStores { _, error in
        if let error { fatalError("Core Data store failed: \(error)") }
    }
    container.viewContext.automaticallyMergesChangesFromParent = true
    container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    return container
}()

automaticallyMergesChangesFromParent = true is critical. Without this, changes saved in background context don't automatically reach viewContext, and NSFetchedResultsController doesn't update UI.

Multithreading — Main Trap

NSManagedObject not thread-safe. Can't pass object between threads — only objectID via NSManagedObjectID. In background context get object copy:

let backgroundContext = persistentContainer.newBackgroundContext()
backgroundContext.perform {
    let objectInBg = backgroundContext.object(with: objectID)
    // change objectInBg
    try? backgroundContext.save()
}

Most common crash: EXC_BAD_ACCESS or NSInternalInconsistencyException on accessing NSManagedObject not in its thread. Instruments → Core Data template shows where this happens.

performAndWait vs perform. perform is async, performAndWait is sync and can deadlock if called from main thread waiting for background context which in turn waits for main. Use perform for background saves.

NSFetchedResultsController and Diffable Data Source

NSFetchedResultsController tracks Core Data changes and notifies delegate. Binding with UICollectionViewDiffableDataSource works via controllerDidChangeContent:

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    var snapshot = NSDiffableDataSourceSnapshot<Section, NSManagedObjectID>()
    snapshot.appendSections([.main])
    snapshot.appendItems(controller.fetchedObjects?.map(\.objectID) ?? [])
    dataSource.apply(snapshot, animatingDifferences: true)
}

Use objectID in snapshot, not NSManagedObject itself — otherwise diffable source can't properly compare objects.

Migrations

On model data change migration needed. Easy migration (NSInferMappingModelAutomatically) works for adding/removing attributes. For renames, type changes — custom migration policy via NSEntityMigrationPolicy. Without proper migration loadPersistentStores returns NSMigrationError, app won't start.

Add to configuration:

container.persistentStoreDescriptions.first?.shouldMigrateStoreAutomatically = true
container.persistentStoreDescriptions.first?.shouldInferMappingModelAutomatically = true

CloudKit Synchronization

NSPersistentCloudKitContainer instead of NSPersistentContainer enables iCloud CloudKit sync. Requires: iCloud Entitlement, CloudKit capability in Xcode, model without some attribute types (Binary Data with External Storage doesn't auto-sync).

Sync conflicts resolved via mergePolicyNSMergeByPropertyObjectTrumpMergePolicy usually right choice.

What's Included in Work

  • Create .xcdatamodeld with entity and relationships
  • Configure NSPersistentContainer with right context parameters
  • Background context for data import and writes
  • NSFetchedResultsController for UI data display
  • Migration strategy for future model changes
  • Optional: CloudKit sync

Timeline

Basic stack with one-two entities and NSFetchedResultsController: 1 day. Complex model, migrations, background sync, CloudKit integration: 2–3 days. Cost estimated after data requirements analysis.