设置:
我的应用使用核心数据&云套件镜像。
对于单元测试,我想通过设置 cloudkitContainerOptions = nil
nspersistentStoreDescription
nil nil
要模拟镜像,我想设置一个使用与普通数据堆栈相同持久存储的第二核数据堆栈。
此外,SQL持久存储被 NSINMEMORYSTORETYPE
持续存储所取代。
问题:
我没有设法在2个核心数据堆栈中使用相同的内存中持久存储。
这两个堆栈均使用 nspersistentCloudkitContainer
。
创建
两者都使用相同的文件URL使用相同的 nsperSistentStoredEscription
,尽管此文件URL显然在内存中持久商店被忽略了。
因此,两个容器都使用不同的内存持续存储,并且无法模拟iCloud镜像到单个持久存储。
问题:
i可能的设置可能是可能的,如果是,如何?
ps:我知道我可能可以通过指定相同的文件URL来使用相同的SQL存储。但这有一个缺点,即商店在不同的单位测试之间持续存在,并且必须在每个测试的开始时重置。
Setup:
My app uses core data & cloud kit mirroring.
For unit tests, I want to mock iCloud mirroring by setting cloudKitContainerOptions = nil
of the NSPersistentStoreDescription
of the persistent store used.
To mock mirroring, I want to setup a 2nd core data stack that uses the same persistent store as the normal data stack.
Additionally, the SQL persistent store is replaced by an NSInMemoryStoreType
persistent store.
Problem:
I did not manage to use the same in-memory persistent store for 2 core data stacks.
Both stacks are created with a NSPersistentCloudKitContainer
.
Both use the same NSPersistentStoreDescription
with the same file URL, although this file URL is apparently ignored for an in-memory persistent store.
Thus, both containers use different in-memory persistent stores, and it is not possible to mock iCloud mirroring to a single persistent store.
Question:
I the intended setup possible, and if so, how?
PS: I know that I probably could use the same SQL store by specifying the same file UrL. But this had the disadvantage that the store persisted between different unit tests, and had to be reset at the beginning of each test.
发布评论
评论(1)
确实可以设置2个使用相同内存持续存储的核心数据堆栈,但是它们并非所有属性作为sqlite商店。
这是我针对2个核心数据堆栈的测试设置:
coredatacloudkitcontainer
是nspersistentCloudkitContainer
的子类。它具有静态var manageDobjectModel
,该在init> init
coredatacloudkitcontainer
以及第二个使用的init
nspersistentStoreCoorDinator
(必须仅加载模型一次或核心数据会感到困惑):还请注意
addperSistentStore
使用一个选项[nspersistentestensoryTrackingKey:true]
,如果privateStore
是用private> private> private> prifateStoredEscription.setoption(true为nsnumber,nsnumber,forkey,forkey,forkey,forkey,forkey> privateStore
) :nspersistenthistoryTrackingKey)。如果未设置它,将会获得错误coredata:coredata:故障:在没有NSperSistestoneSteRyTrackingKey的情况下打开的存储,但以前已使用nspersistentesthistorytrackingkey打开 - 强迫仅读取仅读取模式存储'file'file:// ...
< <<代码> privatestore 被初始化为
nssqlitestoreType
forprivateStoreType == .persistentStore
和.nulldevice
,以及nsinmemorystoretype
forprivateStoreType == .inmemory
。请注意,该选项.nulldevice
仅在内存中创建一个SQLite存储,但是.inmemory
创建 在内存中存储了一些,但是它是不是 Sqlite商店。关于.nulldevice
存储的信息可以在。警告:
尽管可以使用相同的内存持续存储设置2个核心数据堆栈,但该商店有局限性。一个是,通过一个堆栈修改商店不会触发
nspersistentStorStoreRemoteChangeNotification
,因此不可能进行iCloud镜像的单元测试。为此,必须使用基于文件的SQLite持久存储。It is indeed possible to set up 2 core data stacks that use the same in-memory persistent store, but they will have not all properties as an SQLite store.
Here is my test setup for the 2 core data stacks:
CoreDataCloudKitContainer
is a subclass ofNSPersistentCloudKitContainer
. It has a staticvar managedObjectModel
that is used duringinit
of aCoreDataCloudKitContainer
as well as theinit
of the 2ndNSPersistentStoreCoordinator
(the model must only be loaded once or core data will get confused):Note also that
addPersistentStore
uses an option[NSPersistentHistoryTrackingKey: true]
that is required ifprivateStore
is defined withprivateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
. If it is not set, one will get an errorCoreData: fault: Store opened without NSPersistentHistoryTrackingKey but previously had been opened with NSPersistentHistoryTrackingKey - Forcing into Read Only mode store at 'file://...
privateStore
is initialized aswhere type is
NSSQLiteStoreType
forprivateStoreType == .persistentStore
and.nullDevice
, andNSInMemoryStoreType
forprivateStoreType == .inMemory
. Note that the option.nullDevice
creates an SQLite store in memory only, but.inMemory
creates some store in memory, but it is not a SQLite store. Infos about a.nullDevice
store can be found in this blog.A warning:
Although it is possible to set up 2 core data stacks using the same in-memory persistent store, such a store has limitations. One is that modifying the store by one stack does not trigger an
NSPersistentStoreRemoteChangeNotification
, so that unit testing of iCloud mirroring is not possible. For that, one has to use a file based SQLite persistent store.