|
@@ -313,8 +313,8 @@ extension CoreDataStack {
|
|
|
propertiesToFetch: [String]? = nil,
|
|
propertiesToFetch: [String]? = nil,
|
|
|
callingFunction: String = #function,
|
|
callingFunction: String = #function,
|
|
|
callingClass: String = #fileID
|
|
callingClass: String = #fileID
|
|
|
- ) -> [T] {
|
|
|
|
|
- let request = NSFetchRequest<T>(entityName: String(describing: type))
|
|
|
|
|
|
|
+ ) -> [Any] {
|
|
|
|
|
+ let request = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing: type))
|
|
|
request.sortDescriptors = [NSSortDescriptor(key: key, ascending: ascending)]
|
|
request.sortDescriptors = [NSSortDescriptor(key: key, ascending: ascending)]
|
|
|
request.predicate = predicate
|
|
request.predicate = predicate
|
|
|
if let limit = fetchLimit {
|
|
if let limit = fetchLimit {
|
|
@@ -323,9 +323,9 @@ extension CoreDataStack {
|
|
|
if let batchSize = batchSize {
|
|
if let batchSize = batchSize {
|
|
|
request.fetchBatchSize = batchSize
|
|
request.fetchBatchSize = batchSize
|
|
|
}
|
|
}
|
|
|
- if let propertiesTofetch = propertiesToFetch {
|
|
|
|
|
- request.propertiesToFetch = propertiesTofetch
|
|
|
|
|
- request.resultType = .managedObjectResultType
|
|
|
|
|
|
|
+ if let propertiesToFetch = propertiesToFetch {
|
|
|
|
|
+ request.propertiesToFetch = propertiesToFetch
|
|
|
|
|
+ request.resultType = .dictionaryResultType
|
|
|
} else {
|
|
} else {
|
|
|
request.resultType = .managedObjectResultType
|
|
request.resultType = .managedObjectResultType
|
|
|
}
|
|
}
|
|
@@ -333,23 +333,22 @@ extension CoreDataStack {
|
|
|
context.name = "fetchContext"
|
|
context.name = "fetchContext"
|
|
|
context.transactionAuthor = "fetchEntities"
|
|
context.transactionAuthor = "fetchEntities"
|
|
|
|
|
|
|
|
- var result: [T]?
|
|
|
|
|
-
|
|
|
|
|
/// we need to ensure that the fetch immediately returns a value as long as the whole app does not use the async await pattern, otherwise we could perform this asynchronously with backgroundContext.perform and not block the thread
|
|
/// we need to ensure that the fetch immediately returns a value as long as the whole app does not use the async await pattern, otherwise we could perform this asynchronously with backgroundContext.perform and not block the thread
|
|
|
- context.performAndWait {
|
|
|
|
|
|
|
+ return context.performAndWait {
|
|
|
do {
|
|
do {
|
|
|
-// debugPrint(
|
|
|
|
|
-// "Fetching \(T.self) in \(callingFunction) from \(callingClass): \(DebuggingIdentifiers.succeeded) on Thread: \(Thread.current)"
|
|
|
|
|
-// )
|
|
|
|
|
- result = try context.fetch(request)
|
|
|
|
|
|
|
+ if propertiesToFetch != nil {
|
|
|
|
|
+ return try context.fetch(request) as? [[String: Any]] ?? []
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return try context.fetch(request) as? [T] ?? []
|
|
|
|
|
+ }
|
|
|
} catch let error as NSError {
|
|
} catch let error as NSError {
|
|
|
debugPrint(
|
|
debugPrint(
|
|
|
"Fetching \(T.self) in \(callingFunction) from \(callingClass): \(DebuggingIdentifiers.failed) \(error) on Thread: \(Thread.current)"
|
|
"Fetching \(T.self) in \(callingFunction) from \(callingClass): \(DebuggingIdentifiers.failed) \(error) on Thread: \(Thread.current)"
|
|
|
)
|
|
)
|
|
|
|
|
+
|
|
|
|
|
+ return []
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return result ?? []
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Fetch Async
|
|
// Fetch Async
|
|
@@ -386,9 +385,6 @@ extension CoreDataStack {
|
|
|
|
|
|
|
|
return await context.perform {
|
|
return await context.perform {
|
|
|
do {
|
|
do {
|
|
|
-// debugPrint(
|
|
|
|
|
-// "Fetching \(T.self) in \(callingFunction) from \(callingClass): \(DebuggingIdentifiers.succeeded) on Thread: \(Thread.current)"
|
|
|
|
|
-// )
|
|
|
|
|
if propertiesToFetch != nil {
|
|
if propertiesToFetch != nil {
|
|
|
return try context.fetch(request) as? [[String: Any]] ?? []
|
|
return try context.fetch(request) as? [[String: Any]] ?? []
|
|
|
} else {
|
|
} else {
|