mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-13 05:45:32 +01:00
WIP
This commit is contained in:
@@ -35,17 +35,19 @@ extension Internals {
|
||||
internal enum AppGroupsManager {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
|
||||
internal typealias AppGroupID = String
|
||||
|
||||
internal typealias BundleID = String
|
||||
|
||||
internal typealias StoreID = UUID
|
||||
internal typealias StorageID = UUID
|
||||
|
||||
@discardableResult
|
||||
internal static func register(
|
||||
appGroupIdentifier: String,
|
||||
appGroupIdentifier: AppGroupID,
|
||||
subdirectory: String?,
|
||||
fileName: String
|
||||
) throws -> StoreID {
|
||||
) throws -> StorageID {
|
||||
|
||||
let bundleID = self.bundleID()
|
||||
let indexMetadataURL = self.indexMetadataURL(
|
||||
@@ -56,7 +58,7 @@ extension Internals {
|
||||
initializer: IndexMetadata.init,
|
||||
{ metadata in
|
||||
|
||||
return metadata.fetchOrCreateStoreID(
|
||||
return metadata.fetchOrCreateStorageID(
|
||||
bundleID: bundleID,
|
||||
subdirectory: subdirectory,
|
||||
fileName: fileName
|
||||
@@ -64,10 +66,30 @@ extension Internals {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
internal static func existingToken(
|
||||
appGroupIdentifier: AppGroupID,
|
||||
bundleID: BundleID,
|
||||
storageID: StorageID
|
||||
) throws -> NSPersistentHistoryToken? {
|
||||
|
||||
let storageMetadataURL = self.storageMetadataURL(
|
||||
appGroupIdentifier: appGroupIdentifier,
|
||||
bundleID: bundleID,
|
||||
storageID: storageID
|
||||
)
|
||||
return try self.metadata(
|
||||
forReadingAt: storageMetadataURL,
|
||||
{ (metadata: StorageMetadata) in
|
||||
|
||||
return metadata.persistentHistoryToken
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
internal static func existingToken(
|
||||
appGroupIdentifier: String,
|
||||
subdirectory: String,
|
||||
appGroupIdentifier: AppGroupID,
|
||||
subdirectory: String?,
|
||||
fileName: String
|
||||
) throws -> NSPersistentHistoryToken? {
|
||||
|
||||
@@ -76,11 +98,11 @@ extension Internals {
|
||||
appGroupIdentifier: appGroupIdentifier
|
||||
)
|
||||
guard
|
||||
let storeID = try self.metadata(
|
||||
let storageID = try self.metadata(
|
||||
forReadingAt: indexMetadataURL,
|
||||
{ (metadata: IndexMetadata) in
|
||||
|
||||
return metadata.fetchStoreID(
|
||||
return metadata.fetchStorageID(
|
||||
bundleID: bundleID,
|
||||
subdirectory: subdirectory,
|
||||
fileName: fileName
|
||||
@@ -91,24 +113,39 @@ extension Internals {
|
||||
|
||||
return nil
|
||||
}
|
||||
return try self.existingToken(
|
||||
appGroupIdentifier: appGroupIdentifier,
|
||||
bundleID: bundleID,
|
||||
storageID: storageID
|
||||
)
|
||||
}
|
||||
|
||||
internal static func setExistingToken(
|
||||
_ newToken: NSPersistentHistoryToken,
|
||||
appGroupIdentifier: AppGroupID,
|
||||
bundleID: BundleID,
|
||||
storageID: StorageID
|
||||
) throws {
|
||||
|
||||
let storageMetadataURL = self.storageMetadataURL(
|
||||
appGroupIdentifier: appGroupIdentifier,
|
||||
bundleID: bundleID,
|
||||
storeID: storeID
|
||||
storageID: storageID
|
||||
)
|
||||
return try self.metadata(
|
||||
forReadingAt: storageMetadataURL,
|
||||
{ (metadata: StorageMetadata) in
|
||||
|
||||
return metadata.persistentHistoryToken
|
||||
try self.metadata(
|
||||
forWritingAt: storageMetadataURL,
|
||||
initializer: StorageMetadata.init,
|
||||
{ metadata in
|
||||
|
||||
metadata.persistentHistoryToken = newToken
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
internal static func setExistingToken(
|
||||
_ newToken: NSPersistentHistoryToken,
|
||||
appGroupIdentifier: String,
|
||||
subdirectory: String,
|
||||
appGroupIdentifier: AppGroupID,
|
||||
subdirectory: String?,
|
||||
fileName: String
|
||||
) throws {
|
||||
|
||||
@@ -117,11 +154,11 @@ extension Internals {
|
||||
appGroupIdentifier: appGroupIdentifier
|
||||
)
|
||||
guard
|
||||
let storeID = try self.metadata(
|
||||
let storageID = try self.metadata(
|
||||
forReadingAt: indexMetadataURL,
|
||||
{ (metadata: IndexMetadata) in
|
||||
|
||||
return metadata.fetchStoreID(
|
||||
return metadata.fetchStorageID(
|
||||
bundleID: bundleID,
|
||||
subdirectory: subdirectory,
|
||||
fileName: fileName
|
||||
@@ -132,18 +169,11 @@ extension Internals {
|
||||
|
||||
return
|
||||
}
|
||||
let storageMetadataURL = self.storageMetadataURL(
|
||||
try self.setExistingToken(
|
||||
newToken,
|
||||
appGroupIdentifier: appGroupIdentifier,
|
||||
bundleID: bundleID,
|
||||
storeID: storeID
|
||||
)
|
||||
try self.metadata(
|
||||
forWritingAt: storageMetadataURL,
|
||||
initializer: StorageMetadata.init,
|
||||
{ metadata in
|
||||
|
||||
metadata.persistentHistoryToken = newToken
|
||||
}
|
||||
storageID: storageID
|
||||
)
|
||||
}
|
||||
|
||||
@@ -151,7 +181,7 @@ extension Internals {
|
||||
// MARK: Private
|
||||
|
||||
private static func appGroupContainerURL(
|
||||
appGroupIdentifier: String
|
||||
appGroupIdentifier: AppGroupID
|
||||
) -> URL {
|
||||
|
||||
guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier) else {
|
||||
@@ -163,7 +193,7 @@ extension Internals {
|
||||
}
|
||||
|
||||
private static func indexMetadataURL(
|
||||
appGroupIdentifier: String
|
||||
appGroupIdentifier: AppGroupID
|
||||
) -> URL {
|
||||
|
||||
return self.appGroupContainerURL(appGroupIdentifier: appGroupIdentifier)
|
||||
@@ -171,15 +201,15 @@ extension Internals {
|
||||
}
|
||||
|
||||
private static func storageMetadataURL(
|
||||
appGroupIdentifier: String,
|
||||
appGroupIdentifier: AppGroupID,
|
||||
bundleID: BundleID,
|
||||
storeID: StoreID
|
||||
storageID: StorageID
|
||||
) -> URL {
|
||||
|
||||
return self
|
||||
.appGroupContainerURL(appGroupIdentifier: appGroupIdentifier)
|
||||
.appendingPathComponent(bundleID, isDirectory: true)
|
||||
.appendingPathComponent(storeID.uuidString, isDirectory: false)
|
||||
.appendingPathComponent(storageID.uuidString, isDirectory: false)
|
||||
.appendingPathExtension("meta")
|
||||
}
|
||||
|
||||
@@ -314,23 +344,29 @@ extension Internals {
|
||||
|
||||
// MARK: FilePrivate
|
||||
|
||||
fileprivate func fetchStoreID(
|
||||
fileprivate func fetchStorageID(
|
||||
bundleID: BundleID,
|
||||
subdirectory: String?,
|
||||
fileName: String
|
||||
) -> StoreID? {
|
||||
) -> StorageID? {
|
||||
|
||||
let fileTag = Self.createFileTag(subdirectory: subdirectory, fileName: fileName)
|
||||
let fileTag = Self.createFileTag(
|
||||
subdirectory: subdirectory,
|
||||
fileName: fileName
|
||||
)
|
||||
return self.contents[bundleID, default: [:]][fileTag]
|
||||
}
|
||||
|
||||
fileprivate mutating func fetchOrCreateStoreID(
|
||||
fileprivate mutating func fetchOrCreateStorageID(
|
||||
bundleID: BundleID,
|
||||
subdirectory: String?,
|
||||
fileName: String
|
||||
) -> StoreID {
|
||||
|
||||
let fileTag = Self.createFileTag(subdirectory: subdirectory, fileName: fileName)
|
||||
) -> StorageID {
|
||||
|
||||
let fileTag = Self.createFileTag(
|
||||
subdirectory: subdirectory,
|
||||
fileName: fileName
|
||||
)
|
||||
return self.contents[bundleID, default: [:]][fileTag, default: UUID()]
|
||||
}
|
||||
|
||||
|
||||
102
Sources/Internals.PersistentHistoryFetcher.swift
Normal file
102
Sources/Internals.PersistentHistoryFetcher.swift
Normal file
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// Internals.PersistentHistoryFetcher.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2020 John Rommel Estropia
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import Foundation
|
||||
|
||||
|
||||
// MARK: - Internals
|
||||
|
||||
extension Internals {
|
||||
|
||||
// MARK: - PersistentHistoryFetcher
|
||||
|
||||
internal struct PersistentHistoryFetcher {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal let context: NSManagedObjectContext
|
||||
internal let token: NSPersistentHistoryToken?
|
||||
|
||||
internal init(
|
||||
context: NSManagedObjectContext,
|
||||
token: NSPersistentHistoryToken?
|
||||
) {
|
||||
|
||||
self.context = context
|
||||
self.token = token
|
||||
}
|
||||
|
||||
internal func fetch() throws -> [NSPersistentHistoryTransaction] {
|
||||
|
||||
let request = self.newFetchRequest()
|
||||
let fetchResult = try self.context.execute(request) as! NSPersistentHistoryResult
|
||||
return fetchResult.result as! [NSPersistentHistoryTransaction]
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private func newFetchRequest() -> NSPersistentHistoryChangeRequest {
|
||||
|
||||
let historyFetchRequest = NSPersistentHistoryChangeRequest.fetchHistory(
|
||||
after: self.token
|
||||
)
|
||||
guard #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) else {
|
||||
|
||||
return historyFetchRequest
|
||||
}
|
||||
guard let transactionFetchRequest = NSPersistentHistoryTransaction.fetchRequest else {
|
||||
|
||||
return historyFetchRequest
|
||||
}
|
||||
|
||||
let context = self.context
|
||||
transactionFetchRequest.predicate = NSCompoundPredicate(
|
||||
type: .and,
|
||||
subpredicates: [
|
||||
context.transactionAuthor.map { author in
|
||||
NSPredicate(
|
||||
format: "%K != %@",
|
||||
#keyPath(NSPersistentHistoryTransaction.author),
|
||||
author
|
||||
)
|
||||
},
|
||||
context.name.map { contextName in
|
||||
NSPredicate(
|
||||
format: "%K != %@",
|
||||
#keyPath(NSPersistentHistoryTransaction.contextName),
|
||||
contextName
|
||||
)
|
||||
}
|
||||
]
|
||||
.compactMap({ $0 })
|
||||
)
|
||||
historyFetchRequest.fetchRequest = transactionFetchRequest
|
||||
|
||||
return historyFetchRequest
|
||||
}
|
||||
}
|
||||
}
|
||||
149
Sources/Internals.PersistentHistoryObserver.swift
Normal file
149
Sources/Internals.PersistentHistoryObserver.swift
Normal file
@@ -0,0 +1,149 @@
|
||||
//
|
||||
// Internals.PersistentHistoryObserver.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2020 John Rommel Estropia
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import Foundation
|
||||
|
||||
|
||||
// MARK: - Internals
|
||||
|
||||
extension Internals {
|
||||
|
||||
// MARK: - PersistentHistoryObserver
|
||||
|
||||
internal final class PersistentHistoryObserver {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal init(
|
||||
appGroupIdentifier: AppGroupsManager.AppGroupID,
|
||||
bundleID: AppGroupsManager.BundleID,
|
||||
storageID: AppGroupsManager.StorageID,
|
||||
dataStack: DataStack
|
||||
) {
|
||||
|
||||
self.appGroupIdentifier = appGroupIdentifier
|
||||
self.bundleID = bundleID
|
||||
self.storageID = storageID
|
||||
self.dataStack = dataStack
|
||||
}
|
||||
|
||||
internal func startObserving() {
|
||||
|
||||
if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) {
|
||||
|
||||
self.observerForRemoteChangeNotification = Internals.NotificationObserver(
|
||||
notificationName: .NSPersistentStoreRemoteChange,
|
||||
object: self,
|
||||
closure: { [weak self] (note) -> Void in
|
||||
|
||||
guard let `self` = self else {
|
||||
|
||||
return
|
||||
}
|
||||
self.historyQueue.addOperation { [weak self] in
|
||||
|
||||
self?.processPersistentHistory()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
else {
|
||||
|
||||
#warning("TODO: handle remote changes for iOS 11 & 12")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let appGroupIdentifier: AppGroupsManager.AppGroupID
|
||||
private let bundleID: AppGroupsManager.BundleID
|
||||
private let storageID: AppGroupsManager.StorageID
|
||||
private let dataStack: DataStack
|
||||
|
||||
private var observerForRemoteChangeNotification: Internals.NotificationObserver?
|
||||
|
||||
private lazy var historyQueue: OperationQueue = Internals.with {
|
||||
|
||||
let queue = OperationQueue()
|
||||
queue.maxConcurrentOperationCount = 1
|
||||
return queue
|
||||
}
|
||||
|
||||
private func processPersistentHistory() {
|
||||
|
||||
self.dataStack.perform(
|
||||
asynchronous: { transaction -> NSPersistentHistoryToken? in
|
||||
|
||||
let token = try AppGroupsManager.existingToken(
|
||||
appGroupIdentifier: self.appGroupIdentifier,
|
||||
bundleID: self.bundleID,
|
||||
storageID: self.storageID
|
||||
)
|
||||
|
||||
let context = transaction.unsafeContext()
|
||||
let fetcher = PersistentHistoryFetcher(
|
||||
context: context,
|
||||
token: token
|
||||
)
|
||||
let history = try fetcher.fetch()
|
||||
guard !history.isEmpty else {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
context.merge(fromPersistentHistory: history)
|
||||
|
||||
return history.last?.token
|
||||
},
|
||||
success: { token in
|
||||
|
||||
guard let token = token else {
|
||||
|
||||
return
|
||||
}
|
||||
do {
|
||||
|
||||
try AppGroupsManager.setExistingToken(
|
||||
token,
|
||||
appGroupIdentifier: self.appGroupIdentifier,
|
||||
bundleID: self.bundleID,
|
||||
storageID: self.storageID
|
||||
)
|
||||
}
|
||||
catch {
|
||||
|
||||
#warning("TODO: handle error")
|
||||
}
|
||||
},
|
||||
failure: { error in
|
||||
|
||||
#warning("TODO: handle error")
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -232,6 +232,22 @@ extension NSManagedObjectContext {
|
||||
|
||||
self.refreshAllObjects()
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func merge(fromPersistentHistory transactions: [NSPersistentHistoryTransaction]) {
|
||||
|
||||
for transaction in transactions {
|
||||
|
||||
guard let userInfo = transaction.objectIDNotification().userInfo else {
|
||||
|
||||
continue
|
||||
}
|
||||
NSManagedObjectContext.mergeChanges(
|
||||
fromRemoteContextSave: userInfo,
|
||||
into: [self]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
Reference in New Issue
Block a user