2 Replies
      Latest reply on May 19, 2020 11:59 PM by matryx87
      matryx87 Level 1 Level 1 (0 points)

        Hi everyone!
        Here's a little tricky question, at least for me.
        I'm trying to add an apple watch extension to an existing app of mine (wrote in Swift).
        This App uses a CoreData database and i need to retrieve that data to build the Apple Watch UI.
        I decided to go for SwiftUI to code the Watch App, because of the new possibilities in the UI.

        So Main App in Swift and Watch App in SwiftUI

         

        First i created an app group of course, and a class to define a storeURL for the container:

        class AppGroupPersistentContainer: NSPersistentContainer {
        
            override open class func defaultDirectoryURL() -> URL {
                var storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.ManhattanGroup")
                storeURL = storeURL?.appendingPathComponent("Manhattan.sqlite")
                return storeURL!
            }
            
        }

        in my core data manager (a class i created to crud) i placed the persistant container

            // MARK: - PersistentContainer
            lazy var persistentContainer: NSPersistentContainer = {
                let container = AppGroupPersistentContainer(name: "Manhattan")
        
                container.loadPersistentStores(completionHandler: { (storeDescription, error) in
                    if let error = error as NSError? {
                        fatalError("Unresolved error \(error), \(error.userInfo)")
                    }
                })
                return container
            }()

         

        then for each operation i just get the context from the persistent container.

            func getContext() -> NSManagedObjectContext {
                return persistentContainer.viewContext
            }

         

        So far so good i guess.

        In my watch app, i call the context, and use that context for the environment.

        struct ContentView: View {
            var body: some View {
                let managedObjectContext = CoreDataManager.shared.getContext()
                
                return ZStack(alignment: .center) {
                    CategoryView().environment(\.managedObjectContext, managedObjectContext)
                }
            }
        }
        
        

         

        finally in the CategoryView i fetch like this:

        @FetchRequest(entity: CoreList.entity(), sortDescriptors: []) var coreList: FetchedResults
        
         var body: some View {
        
                return VStack(alignment: .leading) {
                    List {
                        ForEach(coreList, id: \.self) { object in
                            Text("\(object)")
                        }
                    }.environment(\.managedObjectContext, managedObjectContext)//not sure i should use this again

         

        and save like this:

        @Environment(\.managedObjectContext) var managedObjectContext
        
         var body: some View {
        
                return VStack(alignment: .leading) {
                  ...     
                  Button("Add") {
                        let coreList = CoreList(context: self.managedObjectContext)
                        
        //coreList init
        
                        try? self.managedObjectContext.save()
                    }
        
        

        I can save and fetch locally successfully but this operations don't take effect on the iPhone App and as you can guess, if i save from the iPhone App i can't find these values on my Watch App.

        Does anyone have an idea? i think something is missing in the configuration of the app group but i just cant figure out.
        Thank you in advance.