在MVVM应用程序中访问核心数据栈

问题描述:

我正在使用 MVVM 模式编写应用程序。而且我想知道如何创建 CoreData 堆栈,以便可以从我的应用程序中的不同位置访问它。

I'm writing an application using the MVVM pattern. And I'm wondering know how to create the CoreData stack so it can be accessed from various places in my app.

第一种方法是在 AppDelegate 中创建一个持久性容器,然后将该服务注入到我的ViewModels中(同时将 managedObjectContext 传递给

First approach is to create a persistent container in the AppDelegate and then inject this service to my ViewModels (simultaneously passing the managedObjectContext as an environment variable to my Views).

但是,这种方式在整个应用程序中访问上下文更加困难:例如在解码网络响应时,因为它们无权访问 managedObjectContext

This way, however, accessing context throughout the app is more difficult: e.g. in decoding network responses, as they don't have access to the managedObjectContext:

protocol APIResource {
    associatedtype Response: Decodable
    ...
}

extension APIResource {
    func decode(_ data: Data) -> AnyPublisher<Response, APIError> {
        Just(data)
            // how can I access context here to pass it to JSONDecoder?
            .decode(type: Response.self, decoder: JSONDecoder())
            .mapError { error in
                .parsing(description: error.localizedDescription)
            }
            .eraseToAnyPublisher()
    }
}

我看到的另一个解决方案是使用单身人士。我可以从项目中的任何位置访问它,但是如何以正确的方式创建它们

The other solution I've seen is to use a singleton. I can access it from anywhere in the project but how can I create it in the right way?

如果我不想在对象中修改某些对象怎么办? main background 同时排队?或者,如果两个队列都想要修改同一对象怎么办?

What if I wan't to modify some object in the main and the background queue at the same time? Or what if both queues want to modify the same object?

您可以使用Core Data Singleton类

You can use Core Data Singleton class

import CoreData

class CoreDataStack {
    static let shared = CoreDataStack()

    private init() {}

    var managedObjectContext: NSManagedObjectContext {
        return self.persistentContainer.viewContext
    }

    var workingContext: NSManagedObjectContext {
        let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
        context.parent = self.managedObjectContext
        return context
    }

    // MARK: - Core Data stack

    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "MyStuff")
        container.loadPersistentStores(completionHandler: { storeDescription, error in
            if let error = error as NSError? {
                RaiseError.raise()
            }
        })
        return container
    }()

    // MARK: - Core Data Saving support

    func saveContext() {
        self.managedObjectContext.performAndWait {
            if self.managedObjectContext.hasChanges {
                do {
                    try self.managedObjectContext.save()
                    appPrint("Main context saved")
                } catch {
                    appPrint(error)
                    RaiseError.raise()
                }
            }
        }
    }

    func saveWorkingContext(context: NSManagedObjectContext) {
        do {
            try context.save()
            appPrint("Working context saved")
            saveContext()
        } catch (let error) {
            appPrint(error)
            RaiseError.raise()
        }
    }
}

核心数据不是线程安全的。如果您在manageObject上写了一些内容,但又不想保存它,但是其他线程保存了上下文,那么您不想保留的更改也将保留。

Core Data is not thread safe. If you write something on manageObject and don't want to save that, but some other thread save the context, then the changes that you don't want to persist will also persist.

所以为避免这种情况,请始终创建工作上下文-这是私有的。

So to avoid this situation always create working context - which is private.

按保存时,将先保存私有上下文,然后再保存主上下文。

When you press save, then first private context get saved and after that you save main context.

在MVVM中,您应该具有DataLayer,ViewModel通过该DataLayer与Core Data单例类进行交互。

In MVVM you should have DataLayer through which your ViewModel interact with Core Data singleton class.