From 27b17fb2c51b711643dd68d6e14a45d94a8f7cab Mon Sep 17 00:00:00 2001 From: Alessandro Date: Tue, 6 Apr 2021 12:57:12 +0200 Subject: [PATCH] Remove objc extensions to work with NSArray (#34) --- CHANGELOG.md | 1 + CoreDataPlus.xcodeproj/project.pbxproj | 78 ++++--------------- Package.swift | 10 +-- Sources/{Core => }/Collection+CoreData.swift | 0 .../NSManagedObjectContext+CoreDataPlus.m | 11 --- .../NSManagedObjectContext+CoreDataPlus.h | 13 ---- Sources/{Core => }/CoreDataPlus.swift | 0 .../{Core => }/FetchedResultsChanges.swift | 0 .../Migration/CoreDataMigration+Step.swift | 0 .../Migration/CoreDataMigration.swift | 0 .../Migration/CoreDataModelVersion.swift | 0 .../NSBatchDeleteResult+Utils.swift | 0 .../NSBatchInsertResult+Utils.swift | 0 .../NSBatchUpdateResult+Utils.swift | 0 .../NSEntityDescription+Utils.swift | 0 Sources/{Core => }/NSFetchRequest+Utils.swift | 0 .../NSFetchRequestResult+CoreData.swift | 10 +-- .../NSFetchRequestResult+Utils.swift | 0 .../{Core => }/NSManagedObject+Utils.swift | 0 .../NSManagedObjectContext+History.swift | 0 .../NSManagedObjectContext+Utils.swift | 9 ++- .../NSPersistentStoreCoordinator+Utils.swift | 0 Sources/{Core => }/NSSet+CoreData.swift | 0 .../Notifications/Notification+Payloads.swift | 0 .../Notifications/Notification+Utils.swift | 0 Support/CoreDataPlus.h | 2 - ...nagedObjectContextInvestigationTests.swift | 8 +- 27 files changed, 35 insertions(+), 107 deletions(-) rename Sources/{Core => }/Collection+CoreData.swift (100%) delete mode 100644 Sources/CoreDataExtensions/NSManagedObjectContext+CoreDataPlus.m delete mode 100644 Sources/CoreDataExtensions/Public/NSManagedObjectContext+CoreDataPlus.h rename Sources/{Core => }/CoreDataPlus.swift (100%) rename Sources/{Core => }/FetchedResultsChanges.swift (100%) rename Sources/{Core => }/Migration/CoreDataMigration+Step.swift (100%) rename Sources/{Core => }/Migration/CoreDataMigration.swift (100%) rename Sources/{Core => }/Migration/CoreDataModelVersion.swift (100%) rename Sources/{Core => }/NSBatchDeleteResult+Utils.swift (100%) rename Sources/{Core => }/NSBatchInsertResult+Utils.swift (100%) rename Sources/{Core => }/NSBatchUpdateResult+Utils.swift (100%) rename Sources/{Core => }/NSEntityDescription+Utils.swift (100%) rename Sources/{Core => }/NSFetchRequest+Utils.swift (100%) rename Sources/{Core => }/NSFetchRequestResult+CoreData.swift (98%) rename Sources/{Core => }/NSFetchRequestResult+Utils.swift (100%) rename Sources/{Core => }/NSManagedObject+Utils.swift (100%) rename Sources/{Core => }/NSManagedObjectContext+History.swift (100%) rename Sources/{Core => }/NSManagedObjectContext+Utils.swift (93%) rename Sources/{Core => }/NSPersistentStoreCoordinator+Utils.swift (100%) rename Sources/{Core => }/NSSet+CoreData.swift (100%) rename Sources/{Core => }/Notifications/Notification+Payloads.swift (100%) rename Sources/{Core => }/Notifications/Notification+Utils.swift (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 653ce8ef..3d198e44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - New CoreData notifications payloads. - History: added new history transactions and changes fetch requests. - History: removed unused APIs. +- Added a method to fetch with batched requests. - New batch inserts methods. - Removed entity and contexts observers. - More tests. diff --git a/CoreDataPlus.xcodeproj/project.pbxproj b/CoreDataPlus.xcodeproj/project.pbxproj index 85aeb7e2..ac653283 100644 --- a/CoreDataPlus.xcodeproj/project.pbxproj +++ b/CoreDataPlus.xcodeproj/project.pbxproj @@ -117,10 +117,6 @@ 23FFB86222EC948C00391D40 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23FFB86122EC948C00391D40 /* Utils.swift */; }; 23FFB86322EC948C00391D40 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23FFB86122EC948C00391D40 /* Utils.swift */; }; 23FFB86422EC948C00391D40 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23FFB86122EC948C00391D40 /* Utils.swift */; }; - D2011A092614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m in Sources */ = {isa = PBXBuildFile; fileRef = D20119EF2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m */; }; - D2011A0A2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m in Sources */ = {isa = PBXBuildFile; fileRef = D20119EF2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m */; }; - D2011A0B2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m in Sources */ = {isa = PBXBuildFile; fileRef = D20119EF2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m */; }; - D2011A0C2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m in Sources */ = {isa = PBXBuildFile; fileRef = D20119EF2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m */; }; D2011A112614E3D9001902FB /* NSBatchDeleteResult+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20119F32614E3D9001902FB /* NSBatchDeleteResult+Utils.swift */; }; D2011A122614E3D9001902FB /* NSBatchDeleteResult+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20119F32614E3D9001902FB /* NSBatchDeleteResult+Utils.swift */; }; D2011A132614E3D9001902FB /* NSBatchDeleteResult+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20119F32614E3D9001902FB /* NSBatchDeleteResult+Utils.swift */; }; @@ -201,10 +197,6 @@ D2011A5E2614E3D9001902FB /* NSFetchRequest+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2011A082614E3D9001902FB /* NSFetchRequest+Utils.swift */; }; D2011A5F2614E3D9001902FB /* NSFetchRequest+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2011A082614E3D9001902FB /* NSFetchRequest+Utils.swift */; }; D2011A602614E3D9001902FB /* NSFetchRequest+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2011A082614E3D9001902FB /* NSFetchRequest+Utils.swift */; }; - D2011AC02614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */ = {isa = PBXBuildFile; fileRef = D20119F12614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.h */; settings = {ATTRIBUTES = (Public, ); }; }; - D2011ACA2614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */ = {isa = PBXBuildFile; fileRef = D20119F12614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.h */; settings = {ATTRIBUTES = (Public, ); }; }; - D2011ACB2614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */ = {isa = PBXBuildFile; fileRef = D20119F12614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.h */; settings = {ATTRIBUTES = (Public, ); }; }; - D2011AD52614E421001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */ = {isa = PBXBuildFile; fileRef = D20119F12614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.h */; settings = {ATTRIBUTES = (Public, ); }; }; D27A2E36255F29D20043B43F /* SampleModelV2.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = D27A2E2D255F29D20043B43F /* SampleModelV2.sqlite */; }; D27A2E37255F29D20043B43F /* SampleModelV2.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = D27A2E2D255F29D20043B43F /* SampleModelV2.sqlite */; }; D27A2E38255F29D20043B43F /* SampleModelV2.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = D27A2E2D255F29D20043B43F /* SampleModelV2.sqlite */; }; @@ -299,8 +291,6 @@ 23EFDE9C1F95FE990038BE75 /* CoreDataPlus Tests tvOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "CoreDataPlus Tests tvOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 23EFDEAB1F95FEB40038BE75 /* CoreDataPlus Tests macOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "CoreDataPlus Tests macOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 23FFB86122EC948C00391D40 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = ""; }; - D20119EF2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObjectContext+CoreDataPlus.m"; sourceTree = ""; }; - D20119F12614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObjectContext+CoreDataPlus.h"; sourceTree = ""; }; D20119F32614E3D9001902FB /* NSBatchDeleteResult+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSBatchDeleteResult+Utils.swift"; sourceTree = ""; }; D20119F42614E3D9001902FB /* NSManagedObjectContext+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+Utils.swift"; sourceTree = ""; }; D20119F52614E3D9001902FB /* NSBatchUpdateResult+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSBatchUpdateResult+Utils.swift"; sourceTree = ""; }; @@ -476,8 +466,23 @@ 23B580851F94FF0900A365C0 /* Sources */ = { isa = PBXGroup; children = ( - D20119EE2614E3D9001902FB /* CoreDataExtensions */, - D20119F22614E3D9001902FB /* Core */, + D20119F32614E3D9001902FB /* NSBatchDeleteResult+Utils.swift */, + D20119F42614E3D9001902FB /* NSManagedObjectContext+Utils.swift */, + D20119F52614E3D9001902FB /* NSBatchUpdateResult+Utils.swift */, + D20119F62614E3D9001902FB /* NSBatchInsertResult+Utils.swift */, + D20119F72614E3D9001902FB /* Collection+CoreData.swift */, + D20119F82614E3D9001902FB /* CoreDataPlus.swift */, + D20119F92614E3D9001902FB /* FetchedResultsChanges.swift */, + D20119FA2614E3D9001902FB /* NSSet+CoreData.swift */, + D20119FB2614E3D9001902FB /* NSFetchRequestResult+Utils.swift */, + D20119FC2614E3D9001902FB /* NSManagedObjectContext+History.swift */, + D20119FD2614E3D9001902FB /* NSPersistentStoreCoordinator+Utils.swift */, + D20119FE2614E3D9001902FB /* NSEntityDescription+Utils.swift */, + D20119FF2614E3D9001902FB /* NSManagedObject+Utils.swift */, + D2011A002614E3D9001902FB /* NSFetchRequestResult+CoreData.swift */, + D2011A012614E3D9001902FB /* Migration */, + D2011A052614E3D9001902FB /* Notifications */, + D2011A082614E3D9001902FB /* NSFetchRequest+Utils.swift */, ); path = Sources; sourceTree = ""; @@ -521,47 +526,6 @@ path = SampleModel; sourceTree = ""; }; - D20119EE2614E3D9001902FB /* CoreDataExtensions */ = { - isa = PBXGroup; - children = ( - D20119EF2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m */, - D20119F02614E3D9001902FB /* Public */, - ); - path = CoreDataExtensions; - sourceTree = ""; - }; - D20119F02614E3D9001902FB /* Public */ = { - isa = PBXGroup; - children = ( - D20119F12614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.h */, - ); - path = Public; - sourceTree = ""; - }; - D20119F22614E3D9001902FB /* Core */ = { - isa = PBXGroup; - children = ( - D20119F32614E3D9001902FB /* NSBatchDeleteResult+Utils.swift */, - D20119F42614E3D9001902FB /* NSManagedObjectContext+Utils.swift */, - D20119F52614E3D9001902FB /* NSBatchUpdateResult+Utils.swift */, - D20119F62614E3D9001902FB /* NSBatchInsertResult+Utils.swift */, - D20119F72614E3D9001902FB /* Collection+CoreData.swift */, - D20119F82614E3D9001902FB /* CoreDataPlus.swift */, - D20119F92614E3D9001902FB /* FetchedResultsChanges.swift */, - D20119FA2614E3D9001902FB /* NSSet+CoreData.swift */, - D20119FB2614E3D9001902FB /* NSFetchRequestResult+Utils.swift */, - D20119FC2614E3D9001902FB /* NSManagedObjectContext+History.swift */, - D20119FD2614E3D9001902FB /* NSPersistentStoreCoordinator+Utils.swift */, - D20119FE2614E3D9001902FB /* NSEntityDescription+Utils.swift */, - D20119FF2614E3D9001902FB /* NSManagedObject+Utils.swift */, - D2011A002614E3D9001902FB /* NSFetchRequestResult+CoreData.swift */, - D2011A012614E3D9001902FB /* Migration */, - D2011A052614E3D9001902FB /* Notifications */, - D2011A082614E3D9001902FB /* NSFetchRequest+Utils.swift */, - ); - path = Core; - sourceTree = ""; - }; D2011A012614E3D9001902FB /* Migration */ = { isa = PBXGroup; children = ( @@ -629,7 +593,6 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( - D2011ACA2614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */, 236350B51F95F0ED00B3A16A /* CoreDataPlus.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -638,7 +601,6 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( - D2011ACB2614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */, 236350B61F95F0EF00B3A16A /* CoreDataPlus.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -647,7 +609,6 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( - D2011AD52614E421001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */, 236350B71F95F0F200B3A16A /* CoreDataPlus.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -656,7 +617,6 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( - D2011AC02614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */, 23B5807B1F94FEDF00A365C0 /* CoreDataPlus.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -966,7 +926,6 @@ D2011A162614E3D9001902FB /* NSManagedObjectContext+Utils.swift in Sources */, D2011A462614E3D9001902FB /* NSFetchRequestResult+CoreData.swift in Sources */, D2011A1E2614E3D9001902FB /* NSBatchInsertResult+Utils.swift in Sources */, - D2011A0A2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m in Sources */, D2011A122614E3D9001902FB /* NSBatchDeleteResult+Utils.swift in Sources */, D2011A2E2614E3D9001902FB /* NSSet+CoreData.swift in Sources */, D2011A562614E3D9001902FB /* Notification+Payloads.swift in Sources */, @@ -994,7 +953,6 @@ D2011A172614E3D9001902FB /* NSManagedObjectContext+Utils.swift in Sources */, D2011A472614E3D9001902FB /* NSFetchRequestResult+CoreData.swift in Sources */, D2011A1F2614E3D9001902FB /* NSBatchInsertResult+Utils.swift in Sources */, - D2011A0B2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m in Sources */, D2011A132614E3D9001902FB /* NSBatchDeleteResult+Utils.swift in Sources */, D2011A2F2614E3D9001902FB /* NSSet+CoreData.swift in Sources */, D2011A572614E3D9001902FB /* Notification+Payloads.swift in Sources */, @@ -1022,7 +980,6 @@ D2011A182614E3D9001902FB /* NSManagedObjectContext+Utils.swift in Sources */, D2011A482614E3D9001902FB /* NSFetchRequestResult+CoreData.swift in Sources */, D2011A202614E3D9001902FB /* NSBatchInsertResult+Utils.swift in Sources */, - D2011A0C2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m in Sources */, D2011A142614E3D9001902FB /* NSBatchDeleteResult+Utils.swift in Sources */, D2011A302614E3D9001902FB /* NSSet+CoreData.swift in Sources */, D2011A582614E3D9001902FB /* Notification+Payloads.swift in Sources */, @@ -1050,7 +1007,6 @@ D2011A152614E3D9001902FB /* NSManagedObjectContext+Utils.swift in Sources */, D2011A452614E3D9001902FB /* NSFetchRequestResult+CoreData.swift in Sources */, D2011A1D2614E3D9001902FB /* NSBatchInsertResult+Utils.swift in Sources */, - D2011A092614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m in Sources */, D2011A112614E3D9001902FB /* NSBatchDeleteResult+Utils.swift in Sources */, D2011A2D2614E3D9001902FB /* NSSet+CoreData.swift in Sources */, D2011A552614E3D9001902FB /* Notification+Payloads.swift in Sources */, diff --git a/Package.swift b/Package.swift index cb63e4c3..e01d5df0 100644 --- a/Package.swift +++ b/Package.swift @@ -39,15 +39,7 @@ let package = Package( .library(name: "CoreDataPlus", targets: ["CoreDataPlus"]) ], targets: [ - // Core features - .target(name: "CoreDataPlus", dependencies: ["CoreDataExtensions"], path: "Sources/Core"), - // Objc Utils - .target(name: "CoreDataExtensions", - dependencies: [], - path: "Sources/CoreDataExtensions", - publicHeadersPath: "Public" - ), - + .target(name: "CoreDataPlus", path: "Sources"), .testTarget(name: "Tests", dependencies: ["CoreDataPlus"], path: "Tests", diff --git a/Sources/Core/Collection+CoreData.swift b/Sources/Collection+CoreData.swift similarity index 100% rename from Sources/Core/Collection+CoreData.swift rename to Sources/Collection+CoreData.swift diff --git a/Sources/CoreDataExtensions/NSManagedObjectContext+CoreDataPlus.m b/Sources/CoreDataExtensions/NSManagedObjectContext+CoreDataPlus.m deleted file mode 100644 index 2d9418a6..00000000 --- a/Sources/CoreDataExtensions/NSManagedObjectContext+CoreDataPlus.m +++ /dev/null @@ -1,11 +0,0 @@ -// CoreDataPlus - -#import "NSManagedObjectContext+CoreDataPlus.h" - -@implementation NSManagedObjectContext (CoreDataPlus) -- (nullable NSArray *)cdp_executeFetchRequest:(nonnull NSFetchRequest *)request error:(NSError *_Nullable* _Nullable)error; -{ - // https://mjtsai.com/blog/2021/03/31/making-nsfetchrequest-fetchbatchsize-work-with-swift/ - return [self executeFetchRequest:request error:error]; -} -@end diff --git a/Sources/CoreDataExtensions/Public/NSManagedObjectContext+CoreDataPlus.h b/Sources/CoreDataExtensions/Public/NSManagedObjectContext+CoreDataPlus.h deleted file mode 100644 index 87683cec..00000000 --- a/Sources/CoreDataExtensions/Public/NSManagedObjectContext+CoreDataPlus.h +++ /dev/null @@ -1,13 +0,0 @@ -// CoreDataPlus - -@import CoreData; - -@interface NSManagedObjectContext (CoreDataPlus) - -/// **CoreDataPlus** -/// -/// Returns an array of objects that meet the criteria specified by a given fetch request. -/// @Note The Swift version returns an *Array* and for performance issues you should prefer using a NSArray* **for batched requests**: https://developer.apple.com/forums/thread/651325 . -- (nullable NSArray *)cdp_executeFetchRequest:(nonnull NSFetchRequest *)request error:(NSError *_Nullable* _Nullable)error; - -@end diff --git a/Sources/Core/CoreDataPlus.swift b/Sources/CoreDataPlus.swift similarity index 100% rename from Sources/Core/CoreDataPlus.swift rename to Sources/CoreDataPlus.swift diff --git a/Sources/Core/FetchedResultsChanges.swift b/Sources/FetchedResultsChanges.swift similarity index 100% rename from Sources/Core/FetchedResultsChanges.swift rename to Sources/FetchedResultsChanges.swift diff --git a/Sources/Core/Migration/CoreDataMigration+Step.swift b/Sources/Migration/CoreDataMigration+Step.swift similarity index 100% rename from Sources/Core/Migration/CoreDataMigration+Step.swift rename to Sources/Migration/CoreDataMigration+Step.swift diff --git a/Sources/Core/Migration/CoreDataMigration.swift b/Sources/Migration/CoreDataMigration.swift similarity index 100% rename from Sources/Core/Migration/CoreDataMigration.swift rename to Sources/Migration/CoreDataMigration.swift diff --git a/Sources/Core/Migration/CoreDataModelVersion.swift b/Sources/Migration/CoreDataModelVersion.swift similarity index 100% rename from Sources/Core/Migration/CoreDataModelVersion.swift rename to Sources/Migration/CoreDataModelVersion.swift diff --git a/Sources/Core/NSBatchDeleteResult+Utils.swift b/Sources/NSBatchDeleteResult+Utils.swift similarity index 100% rename from Sources/Core/NSBatchDeleteResult+Utils.swift rename to Sources/NSBatchDeleteResult+Utils.swift diff --git a/Sources/Core/NSBatchInsertResult+Utils.swift b/Sources/NSBatchInsertResult+Utils.swift similarity index 100% rename from Sources/Core/NSBatchInsertResult+Utils.swift rename to Sources/NSBatchInsertResult+Utils.swift diff --git a/Sources/Core/NSBatchUpdateResult+Utils.swift b/Sources/NSBatchUpdateResult+Utils.swift similarity index 100% rename from Sources/Core/NSBatchUpdateResult+Utils.swift rename to Sources/NSBatchUpdateResult+Utils.swift diff --git a/Sources/Core/NSEntityDescription+Utils.swift b/Sources/NSEntityDescription+Utils.swift similarity index 100% rename from Sources/Core/NSEntityDescription+Utils.swift rename to Sources/NSEntityDescription+Utils.swift diff --git a/Sources/Core/NSFetchRequest+Utils.swift b/Sources/NSFetchRequest+Utils.swift similarity index 100% rename from Sources/Core/NSFetchRequest+Utils.swift rename to Sources/NSFetchRequest+Utils.swift diff --git a/Sources/Core/NSFetchRequestResult+CoreData.swift b/Sources/NSFetchRequestResult+CoreData.swift similarity index 98% rename from Sources/Core/NSFetchRequestResult+CoreData.swift rename to Sources/NSFetchRequestResult+CoreData.swift index b26612bb..fd850529 100644 --- a/Sources/Core/NSFetchRequestResult+CoreData.swift +++ b/Sources/NSFetchRequestResult+CoreData.swift @@ -1,9 +1,6 @@ // CoreDataPlus import CoreData -#if SWIFT_PACKAGE -import CoreDataExtensions -#endif extension NSFetchRequestResult where Self: NSManagedObject { /// **CoreDataPlus** @@ -77,9 +74,12 @@ extension NSFetchRequestResult where Self: NSManagedObject { } /// **CoreDataPlus** - /// + /// /// Performs a configurable fetch request in a context. - /// - Note: For fetch requests with **batching enabled** returning a *NSArray* optimizes memory and performance across your object graph. + /// - Note: When fetching data from Core Data, you don’t always know how many values you’ll be getting back. + /// Core Data solves this problem by using a subclass of `NSArray` that will dynamically pull in data from the underlying store on demand. + /// On the other hand, a Swift `Array` requires having every element in the array all at once, and bridging an `NSArray` to a Swift `Array` requires retrieving every single value. + /// - Warning: **Batched requests** are supported only when returning a `NSArray`. /// /// - Parameters: /// - context: Searched context. diff --git a/Sources/Core/NSFetchRequestResult+Utils.swift b/Sources/NSFetchRequestResult+Utils.swift similarity index 100% rename from Sources/Core/NSFetchRequestResult+Utils.swift rename to Sources/NSFetchRequestResult+Utils.swift diff --git a/Sources/Core/NSManagedObject+Utils.swift b/Sources/NSManagedObject+Utils.swift similarity index 100% rename from Sources/Core/NSManagedObject+Utils.swift rename to Sources/NSManagedObject+Utils.swift diff --git a/Sources/Core/NSManagedObjectContext+History.swift b/Sources/NSManagedObjectContext+History.swift similarity index 100% rename from Sources/Core/NSManagedObjectContext+History.swift rename to Sources/NSManagedObjectContext+History.swift diff --git a/Sources/Core/NSManagedObjectContext+Utils.swift b/Sources/NSManagedObjectContext+Utils.swift similarity index 93% rename from Sources/Core/NSManagedObjectContext+Utils.swift rename to Sources/NSManagedObjectContext+Utils.swift index 15c3614e..7006d9de 100644 --- a/Sources/Core/NSManagedObjectContext+Utils.swift +++ b/Sources/NSManagedObjectContext+Utils.swift @@ -57,13 +57,18 @@ extension NSManagedObjectContext { /// **CoreDataPlus** /// /// Returns an array of objects that meet the criteria specified by a given fetch request. - /// @Note The Swift version returns an *Array* and for performance issues you should prefer using a NSArray* **for batched requests**: https://developer.apple.com/forums/thread/651325 . + /// - Note: When fetching data from Core Data, you don’t always know how many values you’ll be getting back. + /// Core Data solves this problem by using a subclass of `NSArray` that will dynamically pull in data from the underlying store on demand. + /// On the other hand, a Swift `Array` requires having every element in the array all at once, and bridging an `NSArray` to a Swift `Array` requires retrieving every single value. + /// - Warning: **Batched requests** are supported only when returning a `NSArray`. + /// - SeeAlso:https://developer.apple.com/forums/thread/651325) public final func fetchNSArray(_ request: NSFetchRequest) throws -> NSArray { // [...] Similarly for fetch requests with batching enabled, you do not want a Swift Array but instead an NSArray to avoid making an immediate copy of the future. // https://developer.apple.com/forums/thread/651325. // swiftlint:disable force_cast let protocolRequest = request as! NSFetchRequest - return try cdp_execute(protocolRequest) as NSArray + let results = try fetch(protocolRequest) as NSArray + return results } } diff --git a/Sources/Core/NSPersistentStoreCoordinator+Utils.swift b/Sources/NSPersistentStoreCoordinator+Utils.swift similarity index 100% rename from Sources/Core/NSPersistentStoreCoordinator+Utils.swift rename to Sources/NSPersistentStoreCoordinator+Utils.swift diff --git a/Sources/Core/NSSet+CoreData.swift b/Sources/NSSet+CoreData.swift similarity index 100% rename from Sources/Core/NSSet+CoreData.swift rename to Sources/NSSet+CoreData.swift diff --git a/Sources/Core/Notifications/Notification+Payloads.swift b/Sources/Notifications/Notification+Payloads.swift similarity index 100% rename from Sources/Core/Notifications/Notification+Payloads.swift rename to Sources/Notifications/Notification+Payloads.swift diff --git a/Sources/Core/Notifications/Notification+Utils.swift b/Sources/Notifications/Notification+Utils.swift similarity index 100% rename from Sources/Core/Notifications/Notification+Utils.swift rename to Sources/Notifications/Notification+Utils.swift diff --git a/Support/CoreDataPlus.h b/Support/CoreDataPlus.h index 4a48dabd..2a343e1f 100644 --- a/Support/CoreDataPlus.h +++ b/Support/CoreDataPlus.h @@ -9,5 +9,3 @@ FOUNDATION_EXPORT double CoreDataPlusVersionNumber; FOUNDATION_EXPORT const unsigned char CoreDataPlusVersionString[]; // In this header, you should import all the public headers of your framework using statements like #import - -#import diff --git a/Tests/NSManagedObjectContextInvestigationTests.swift b/Tests/NSManagedObjectContextInvestigationTests.swift index 3d7e6f84..a33657e5 100644 --- a/Tests/NSManagedObjectContextInvestigationTests.swift +++ b/Tests/NSManagedObjectContextInvestigationTests.swift @@ -408,12 +408,12 @@ final class NSManagedObjectContextInvestigationTests: CoreDataPlusInMemoryTestCa // Running a Swift fetch request with a batch size doesn't work, you have to find a way to fallback to Obj-C // https://mjtsai.com/blog/2021/03/31/making-nsfetchrequest-fetchbatchsize-work-with-swift/ // https://developer.apple.com/forums/thread/651325 - // This fetch will execute SELECT with LIMIT 10 as many times as needed to fetch all the cars ❌ - - //let cars_batchSize_not_working = try Car.fetch(in: context) { $0.fetchBatchSize = 10 } - + // This fetch will execute SELECT with LIMIT 10 just one time ✅ // let cars_batchLimit_working = try Car.fetch(in: context) { $0.fetchLimit = 10 } + + // This fetch will execute SELECT with LIMIT 10 as many times as needed to fetch all the cars ❌ + //let cars_batchSize_not_working = try Car.fetch(in: context) { $0.fetchBatchSize = 10 } // cars is a _PFBatchFaultingArray proxy let cars = try Car.fetchNSArray(in: context) { $0.fetchBatchSize = 10 }