@@ -20,14 +20,23 @@ final class SharedPersistentContainer: NSPersistentContainer {
20
20
extension SharedPersistentContainer : @unchecked Sendable { }
21
21
#endif
22
22
23
- class SharedCoreDataStack {
23
+ public final class SharedCoreDataStack {
24
24
25
25
// MARK: - Private Properties
26
26
27
- fileprivate let modelName : String
27
+ /// - warning: Has to be loaded exactly once per process.
28
+ nonisolated ( unsafe) static let model : NSManagedObjectModel = {
29
+ guard let modelURL = Bundle . module. url ( forResource: " Extensions " , withExtension: " momd " ) else {
30
+ fatalError ( " Core Data model missing " )
31
+ }
32
+ guard let model = NSManagedObjectModel ( contentsOf: modelURL) else {
33
+ fatalError ( " failed to load model " )
34
+ }
35
+ return model
36
+ } ( )
28
37
29
38
fileprivate lazy var storeContainer : SharedPersistentContainer = {
30
- let container = SharedPersistentContainer ( name: self . modelName )
39
+ let container = SharedPersistentContainer ( name: " SharedCoreDataStack " , managedObjectModel : SharedCoreDataStack . model )
31
40
container. loadPersistentStores { ( storeDescription, error) in
32
41
if let error = error as NSError ? {
33
42
DDLogError ( " Error loading persistent stores: \( error) , \( error. userInfo) " )
@@ -40,34 +49,21 @@ class SharedCoreDataStack {
40
49
41
50
/// Returns the managed context associated with the main queue
42
51
///
43
- lazy var managedContext : NSManagedObjectContext = {
52
+ public lazy var managedContext : NSManagedObjectContext = {
44
53
return self . storeContainer. viewContext
45
54
} ( )
46
55
47
56
// MARK: - Initializers
48
57
49
58
/// Initialize the SharedPersistentContainer using the standard Extensions model.
50
59
///
51
- convenience init ( ) {
52
- self . init ( modelName: Constants . sharedModelName)
53
- }
54
-
55
- /// Initialize the core data stack with the given model name.
56
- ///
57
- /// This initializer is meant for testing. You probably want to use the convenience `init()` that uses the standard Extensions model
58
- ///
59
- /// - Parameters:
60
- /// - modelName: Name of the model to initialize the SharedPersistentContainer with.
61
- ///
62
- init ( modelName: String ) {
63
- self . modelName = modelName
64
- }
60
+ public init ( ) { }
65
61
66
62
// MARK: - Public Funcntions
67
63
68
64
/// Commit unsaved changes (if any exist) using the main queue's managed context
69
65
///
70
- func saveContext( ) {
66
+ public func saveContext( ) {
71
67
guard managedContext. hasChanges else {
72
68
return
73
69
}
@@ -89,7 +85,7 @@ extension SharedCoreDataStack {
89
85
/// - Parameter sessionID: the session ID
90
86
/// - Returns: group ID or nil if session does not have an associated group
91
87
///
92
- func fetchGroupID( for sessionID: String ) -> String ? {
88
+ public func fetchGroupID( for sessionID: String ) -> String ? {
93
89
let request = NSFetchRequest < NSFetchRequestResult > ( entityName: " UploadOperation " )
94
90
request. predicate = NSPredicate ( format: " (backgroundSessionIdentifier == %@) " , sessionID)
95
91
request. fetchLimit = 1
@@ -104,7 +100,7 @@ extension SharedCoreDataStack {
104
100
/// - Parameter objectID: Managed object ID string for a given post upload op
105
101
/// - Returns: PostUploadOperation or nil
106
102
///
107
- func fetchPostUploadOp( withObjectID objectID: String ) -> PostUploadOperation ? {
103
+ public func fetchPostUploadOp( withObjectID objectID: String ) -> PostUploadOperation ? {
108
104
guard let storeCoordinator = managedContext. persistentStoreCoordinator,
109
105
let url = URL ( string: objectID) ,
110
106
let managedObjectID = storeCoordinator. managedObjectID ( forURIRepresentation: url) else {
@@ -119,7 +115,7 @@ extension SharedCoreDataStack {
119
115
/// - Parameter postUploadOpObjectID: Managed object ID for a given post upload op
120
116
/// - Returns: PostUploadOperation or nil
121
117
///
122
- func fetchPostUploadOp( withObjectID postUploadOpObjectID: NSManagedObjectID ) -> PostUploadOperation ? {
118
+ public func fetchPostUploadOp( withObjectID postUploadOpObjectID: NSManagedObjectID ) -> PostUploadOperation ? {
123
119
var postUploadOp : PostUploadOperation ?
124
120
do {
125
121
postUploadOp = try managedContext. existingObject ( with: postUploadOpObjectID) as? PostUploadOperation
@@ -136,7 +132,7 @@ extension SharedCoreDataStack {
136
132
/// - Parameter groupID: group ID for a set of upload ops
137
133
/// - Returns: post PostUploadOperation or nil
138
134
///
139
- func fetchPostUploadOp( for groupID: String ) -> PostUploadOperation ? {
135
+ public func fetchPostUploadOp( for groupID: String ) -> PostUploadOperation ? {
140
136
let request = NSFetchRequest < NSFetchRequestResult > ( entityName: " PostUploadOperation " )
141
137
request. predicate = NSPredicate ( format: " (groupID == %@) " , groupID)
142
138
request. fetchLimit = 1
@@ -151,7 +147,7 @@ extension SharedCoreDataStack {
151
147
/// - Parameter groupID: group ID for a set of upload ops
152
148
/// - Returns: An array of MediaUploadOperations or nil
153
149
///
154
- func fetchMediaUploadOps( for groupID: String ) -> [ MediaUploadOperation ] ? {
150
+ public func fetchMediaUploadOps( for groupID: String ) -> [ MediaUploadOperation ] ? {
155
151
let request = NSFetchRequest < NSFetchRequestResult > ( entityName: " MediaUploadOperation " )
156
152
request. predicate = NSPredicate ( format: " (groupID == %@) " , groupID)
157
153
guard let results = ( try ? managedContext. fetch ( request) ) as? [ MediaUploadOperation ] else {
@@ -168,7 +164,7 @@ extension SharedCoreDataStack {
168
164
/// - sessionID: background session ID
169
165
/// - Returns: MediaUploadOperation or nil
170
166
///
171
- func fetchMediaUploadOp( for fileName: String , with sessionID: String ) -> MediaUploadOperation ? {
167
+ public func fetchMediaUploadOp( for fileName: String , with sessionID: String ) -> MediaUploadOperation ? {
172
168
guard let fileNameWithoutExtension = URL ( string: fileName) ? . deletingPathExtension ( ) . lastPathComponent else {
173
169
return nil
174
170
}
@@ -192,7 +188,7 @@ extension SharedCoreDataStack {
192
188
/// - sessionID: background session ID
193
189
/// - Returns: An array of UploadOperations or nil
194
190
///
195
- func fetchSessionUploadOps( for taskIdentifier: Int , with sessionID: String ) -> [ UploadOperation ] ? {
191
+ public func fetchSessionUploadOps( for taskIdentifier: Int , with sessionID: String ) -> [ UploadOperation ] ? {
196
192
var uploadOps : [ UploadOperation ] ?
197
193
let request = NSFetchRequest < NSFetchRequestResult > ( entityName: " UploadOperation " )
198
194
request. predicate = NSPredicate ( format: " (backgroundSessionTaskID == %d AND backgroundSessionIdentifier == %@) " , taskIdentifier, sessionID)
@@ -216,7 +212,7 @@ extension SharedCoreDataStack {
216
212
/// - status: New status
217
213
/// - uploadOpObjectID: Managed object ID for a given upload op
218
214
///
219
- func updateStatus( _ status: UploadOperation . UploadStatus , forUploadOpWithObjectID uploadOpObjectID: NSManagedObjectID ) {
215
+ public func updateStatus( _ status: UploadOperation . UploadStatus , forUploadOpWithObjectID uploadOpObjectID: NSManagedObjectID ) {
220
216
var uploadOp : UploadOperation ?
221
217
do {
222
218
uploadOp = try managedContext. existingObject ( with: uploadOpObjectID) as? UploadOperation
@@ -238,7 +234,7 @@ extension SharedCoreDataStack {
238
234
/// - status: New status
239
235
/// - Returns: Managed object ID of newly saved media upload operation object
240
236
///
241
- func saveMediaOperation( _ remoteMedia: RemoteMedia , sessionID: String , groupIdentifier: String , siteID: NSNumber , with status: UploadOperation . UploadStatus ) -> NSManagedObjectID {
237
+ public func saveMediaOperation( _ remoteMedia: RemoteMedia , sessionID: String , groupIdentifier: String , siteID: NSNumber , with status: UploadOperation . UploadStatus ) -> NSManagedObjectID {
242
238
let mediaUploadOp = MediaUploadOperation ( context: managedContext)
243
239
mediaUploadOp. updateWithMedia ( remote: remoteMedia)
244
240
mediaUploadOp. backgroundSessionIdentifier = sessionID
@@ -262,7 +258,7 @@ extension SharedCoreDataStack {
262
258
/// - remoteMediaID: remote media ID
263
259
/// - remoteURL: remote media URL string
264
260
///
265
- func updateMediaOperation( for fileName: String , with sessionID: String , remoteMediaID: Int64 ? , remoteURL: String ? , width: Int32 ? , height: Int32 ? ) {
261
+ public func updateMediaOperation( for fileName: String , with sessionID: String , remoteMediaID: Int64 ? , remoteURL: String ? , width: Int32 ? , height: Int32 ? ) {
266
262
guard let mediaUploadOp = fetchMediaUploadOp ( for: fileName, with: sessionID) else {
267
263
DDLogError ( " Error loading UploadOperation Object with File Name: \( fileName) " )
268
264
return
@@ -289,7 +285,7 @@ extension SharedCoreDataStack {
289
285
/// - status: New status
290
286
/// - Returns: Managed object ID of newly saved post upload operation object
291
287
///
292
- func savePostOperation( _ remotePost: RemotePost , groupIdentifier: String , with status: UploadOperation . UploadStatus ) -> NSManagedObjectID {
288
+ public func savePostOperation( _ remotePost: RemotePost , groupIdentifier: String , with status: UploadOperation . UploadStatus ) -> NSManagedObjectID {
293
289
let postUploadOp = PostUploadOperation ( context: managedContext)
294
290
postUploadOp. updateWithPost ( remote: remotePost)
295
291
postUploadOp. groupID = groupIdentifier
@@ -307,7 +303,7 @@ extension SharedCoreDataStack {
307
303
/// - remotePostID: New remote post ID
308
304
/// - postUploadOpObjectID: Managed object ID for a given post upload op
309
305
///
310
- func updatePostOperation( with status: UploadOperation . UploadStatus , remotePostID: Int64 , forPostUploadOpWithObjectID postUploadOpObjectID: NSManagedObjectID ) {
306
+ public func updatePostOperation( with status: UploadOperation . UploadStatus , remotePostID: Int64 , forPostUploadOpWithObjectID postUploadOpObjectID: NSManagedObjectID ) {
311
307
guard let postUploadOp = ( try ? managedContext. existingObject ( with: postUploadOpObjectID) ) as? PostUploadOperation else {
312
308
DDLogError ( " Error loading PostUploadOperation Object with ID: \( postUploadOpObjectID) " )
313
309
return
@@ -323,7 +319,7 @@ extension SharedCoreDataStack {
323
319
/// - taskID: New background session task ID
324
320
/// - uploadOpObjectID: Managed object ID for a given upload op
325
321
///
326
- func updateTaskID( _ taskID: NSNumber , forUploadOpWithObjectID uploadOpObjectID: NSManagedObjectID ) {
322
+ public func updateTaskID( _ taskID: NSNumber , forUploadOpWithObjectID uploadOpObjectID: NSManagedObjectID ) {
327
323
var uploadOp : UploadOperation ?
328
324
do {
329
325
uploadOp = try managedContext. existingObject ( with: uploadOpObjectID) as? UploadOperation
0 commit comments