9

🤓 So...I have an app with StoreData and iCloud enabled. My data is syncing between the devices but I don't ge the behaviour I want when it comes to reflecting the changes in the UI.

The behaviour I want: When a user has my app open on two devices (A & B) and makes a change one one (A) of them I want the change to automatically reflect in the second (B) device UI within some reasonable time.

enter image description here

The behaviour I currently have: When the user makes changes on one device (A) nothing ever happens on the other (B)...until I minimise and maximise the app on the second device (B) (Not restarting...just minimising).

enter image description here

🤔 My thought are that the app won't receive the changes (iCloud won't sync) until some action is triggered when minimising and reopening the app.

🥺 But I don't know. According to Syncing a Core Data Store with CloudKit syncing and notifying other devices should happened automatically. And I think my View should be hooked up correctly as it will change instantly when making local changes to data in the CoreData database.

My SwiftUI view pretty much looks like this:

import SwiftUI
import CloudKit
import Foundation

struct MyView: View {
    @FetchRequest(fetchRequest: Entity.fetchRequest() as! NSFetchRequest<Entity>) var entities: FetchedResults<Entity>

    var body: some View {
        List {
            ForEach(entities, id: \.id) { e in
                Text("\(e.title)")
            }
        }
    }
}

public class Entity: NSManagedObject {
    @NSManaged public var id: UUID?
    @NSManaged public var title: String
}

Any Help is much appreciated! (Let me know if I need to provide additional information.)

9
  • Check this answer stackoverflow.com/questions/58860967/… Mar 31, 2020 at 21:09
  • 1
    @AlbertoLunardini sadly I already have "context.automaticallyMergesChangesFromParent = true" and It does not make any difference.
    – iSebbeYT
    Apr 5, 2020 at 18:53
  • Do you by any chance use a simulator and a real device to test? I have found that the simulator does not update when you make a change on the real device, but the real device will update when you make a change on the simulator (XCode 11.4).
    – Ch Ryder
    Apr 6, 2020 at 10:34
  • 1
    @Charles I am using an iPhoneX and an iPad Pro. Both running 13.4
    – iSebbeYT
    Apr 6, 2020 at 10:49
  • I see. And have you also tried try? context.setQueryGenerationFrom(.current). For every time you access the context, not just once?
    – Ch Ryder
    Apr 6, 2020 at 11:02

1 Answer 1

2
// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentCloudKitContainer = {

    let container = NSPersistentCloudKitContainer(name: "ComCloudSync")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

Try adding the following code (change "ComCloudSync" to your appName):

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentCloudKitContainer = {

    let container = NSPersistentCloudKitContainer(name: "ComCloudSync")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })

    container.viewContext.automaticallyMergesChangesFromParent = true
    container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy

  return container
}()
1
  • thanks this helped! BUT why is it pointing only to the Private database? i tried storeDescription.cloudKitContainerOptions?.databaseScope = .public in my Presistence.swift file but this didnt work May 1, 2021 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.