Skip to content

Commit

Permalink
Remove objc extensions to work with NSArray (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
alemar11 authored Apr 6, 2021
1 parent 649a615 commit 27b17fb
Show file tree
Hide file tree
Showing 27 changed files with 35 additions and 107 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
78 changes: 17 additions & 61 deletions CoreDataPlus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
D20119EF2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObjectContext+CoreDataPlus.m"; sourceTree = "<group>"; };
D20119F12614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObjectContext+CoreDataPlus.h"; sourceTree = "<group>"; };
D20119F32614E3D9001902FB /* NSBatchDeleteResult+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSBatchDeleteResult+Utils.swift"; sourceTree = "<group>"; };
D20119F42614E3D9001902FB /* NSManagedObjectContext+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+Utils.swift"; sourceTree = "<group>"; };
D20119F52614E3D9001902FB /* NSBatchUpdateResult+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSBatchUpdateResult+Utils.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -521,47 +526,6 @@
path = SampleModel;
sourceTree = "<group>";
};
D20119EE2614E3D9001902FB /* CoreDataExtensions */ = {
isa = PBXGroup;
children = (
D20119EF2614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.m */,
D20119F02614E3D9001902FB /* Public */,
);
path = CoreDataExtensions;
sourceTree = "<group>";
};
D20119F02614E3D9001902FB /* Public */ = {
isa = PBXGroup;
children = (
D20119F12614E3D9001902FB /* NSManagedObjectContext+CoreDataPlus.h */,
);
path = Public;
sourceTree = "<group>";
};
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 = "<group>";
};
D2011A012614E3D9001902FB /* Migration */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -629,7 +593,6 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
D2011ACA2614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */,
236350B51F95F0ED00B3A16A /* CoreDataPlus.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -638,7 +601,6 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
D2011ACB2614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */,
236350B61F95F0EF00B3A16A /* CoreDataPlus.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -647,7 +609,6 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
D2011AD52614E421001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */,
236350B71F95F0F200B3A16A /* CoreDataPlus.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -656,7 +617,6 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
D2011AC02614E420001902FB /* NSManagedObjectContext+CoreDataPlus.h in Headers */,
23B5807B1F94FEDF00A365C0 /* CoreDataPlus.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
10 changes: 1 addition & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
File renamed without changes.
11 changes: 0 additions & 11 deletions Sources/CoreDataExtensions/NSManagedObjectContext+CoreDataPlus.m

This file was deleted.

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// CoreDataPlus

import CoreData
#if SWIFT_PACKAGE
import CoreDataExtensions
#endif

extension NSFetchRequestResult where Self: NSManagedObject {
/// **CoreDataPlus**
Expand Down Expand Up @@ -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.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ request: NSFetchRequest<T>) 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<NSFetchRequestResult>
return try cdp_execute(protocolRequest) as NSArray
let results = try fetch(protocolRequest) as NSArray
return results
}
}

Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions Support/CoreDataPlus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CoreDataPlus/PublicHeader.h>

#import <CoreDataPlus/NSManagedObjectContext+CoreDataPlus.h>
8 changes: 4 additions & 4 deletions Tests/NSManagedObjectContextInvestigationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 27b17fb

Please sign in to comment.