Skip to content

Commit

Permalink
Merge pull request #1 from lobodart/dev
Browse files Browse the repository at this point in the history
Swift3 Update
  • Loading branch information
lobodart authored Sep 19, 2016
2 parents e6eddae + 9d5851c commit 0140bbe
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
5 changes: 2 additions & 3 deletions CoreDataHelper.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "CoreDataHelper"
s.version = "1.0"
s.version = "1.1"
s.summary = "CoreDataHelper"

# This description is used to generate tags and improve search results.
Expand All @@ -29,7 +29,6 @@ Pod::Spec.new do |s|
DESC

s.homepage = "https://github.com/lobodart/CoreDataHelper"
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down Expand Up @@ -78,7 +77,7 @@ Pod::Spec.new do |s|
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/lobodart/CoreDataHelper.git", :tag => "v1.0" }
s.source = { :git => "https://github.com/lobodart/CoreDataHelper.git", :tag => "v1.1" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
24 changes: 21 additions & 3 deletions CoreDataHelper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,17 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Louis Bodart";
TargetAttributes = {
48A500DE1C9227080000B0F9 = {
CreatedOnToolsVersion = 7.2.1;
LastSwiftMigration = 0800;
};
48A500E81C9227080000B0F9 = {
CreatedOnToolsVersion = 7.2.1;
DevelopmentTeam = 58G6JMC92R;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -219,8 +222,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -243,7 +248,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand All @@ -267,8 +272,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -285,9 +292,10 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -299,6 +307,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -311,6 +320,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -319,6 +329,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -330,27 +341,32 @@
PRODUCT_BUNDLE_IDENTIFIER = fr.louisbodart.CoreDataHelper;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
48A500F71C9227080000B0F9 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = 58G6JMC92R;
INFOPLIST_FILE = CoreDataHelperTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = fr.louisbodart.CoreDataHelperTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
48A500F81C9227080000B0F9 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = 58G6JMC92R;
INFOPLIST_FILE = CoreDataHelperTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = fr.louisbodart.CoreDataHelperTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -373,6 +389,7 @@
48A500F51C9227080000B0F9 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
48A500F61C9227080000B0F9 /* Build configuration list for PBXNativeTarget "CoreDataHelperTests" */ = {
isa = XCConfigurationList;
Expand All @@ -381,6 +398,7 @@
48A500F81C9227080000B0F9 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
70 changes: 29 additions & 41 deletions CoreDataHelper/CoreDataHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import Foundation
import CoreData

// MARK: - CoreDataHelper entity protocol
public protocol CDHelperEntity: class {
public protocol CDHelperEntity: class, NSFetchRequestResult {
static var entityName: String! { get }
}

// MARK: - CoreDataHelper entity protocol extension
public extension CDHelperEntity {

// MARK: Private static variables
private static var mainContext: NSManagedObjectContext! {
fileprivate static var mainContext: NSManagedObjectContext! {
let context: NSManagedObjectContext! = CDHelper.mainContext

assert((context != nil), "CDHelper error: mainContext must be set in the AppDelegate.")
Expand All @@ -35,7 +35,7 @@ public extension CDHelperEntity {
/// Delete the entity from the main context.
public func destroy() {
if let object = self as? NSManagedObject {
Self.mainContext.deleteObject(object)
Self.mainContext.delete(object)
self.save()
}
}
Expand All @@ -47,7 +47,7 @@ public extension CDHelperEntity {
/// - returns:
/// A freshly created entity.
public static func new() -> Self {
let newEntity: Self = NSEntityDescription.insertNewObjectForEntityForName(self.entityName, inManagedObjectContext: Self.mainContext) as! Self
let newEntity: Self = NSEntityDescription.insertNewObject(forEntityName: self.entityName, into: Self.mainContext) as! Self
return newEntity
}

Expand All @@ -57,7 +57,7 @@ public extension CDHelperEntity {
/// - Dictionary: A dictionary of data used to fill your entity
/// - returns:
/// A freshly created entity filled with the data.
public static func new(data: [String: AnyObject?]) -> Self {
public static func new(_ data: [String: Any?]) -> Self {
let newEntity: NSManagedObject = self.new() as! NSManagedObject
let availableKeys = newEntity.entity.attributesByName.keys

Expand All @@ -76,30 +76,30 @@ public extension CDHelperEntity {
return self._fetchUsingFetchRequest(self._buildFindAllRequest(usingSortDescriptors: sortDescriptors))
}

public static func findOne(predicate: String) -> Self? {
public static func findOne(_ predicate: String) -> Self? {
return self._fetchUsingFetchRequest(self._buildFindOneRequest(predicate)).first
}

public static func find(predicate: String, usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil, limit fetchLimit: Int! = nil) -> [Self] {
public static func find(_ predicate: String, usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil, limit fetchLimit: Int! = nil) -> [Self] {
return self._fetchUsingFetchRequest(self._buildFindRequest(predicate, usingSortDescriptors: sortDescriptors, limit: fetchLimit))
}

public static func asynchronouslyFindAll(usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil, completion: ([Self]) -> Void) {
self._asynchronouslyFetchUsingRequest(self._buildFindAllRequest(usingSortDescriptors: sortDescriptors), completion: completion)
}

public static func asynchronouslyFindOne(predicate: String, completion: (Self?) -> Void) {
public static func asynchronouslyFindOne(_ predicate: String, completion: (Self?) -> Void) {
self._asynchronouslyFetchUsingRequest(self._buildFindOneRequest(predicate), completion: completion)
}

public static func asynchronouslyFind(predicate: String, usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil, limit fetchLimit: Int! = nil, completion: ([Self]) -> Void) {
public static func asynchronouslyFind(_ predicate: String, usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil, limit fetchLimit: Int! = nil, completion: ([Self]) -> Void) {
self._asynchronouslyFetchUsingRequest(self._buildFindRequest(predicate, usingSortDescriptors: sortDescriptors, limit: fetchLimit), completion: completion)
}

// MARK: Private class methods
private static func _buildFetchRequestUsingPredicate(predicate: String!, sortDescriptors: [NSSortDescriptor]!, fetchLimit: Int!) -> NSFetchRequest {
let fetchRequest: NSFetchRequest = NSFetchRequest()
let entity: NSEntityDescription! = NSEntityDescription.entityForName(self.entityName, inManagedObjectContext: Self.mainContext)
fileprivate static func _buildFetchRequestUsingPredicate(_ predicate: String!, sortDescriptors: [NSSortDescriptor]!, fetchLimit: Int!) -> NSFetchRequest<Self> {
let fetchRequest: NSFetchRequest = NSFetchRequest<Self>()
let entity: NSEntityDescription! = NSEntityDescription.entity(forEntityName: self.entityName, in: Self.mainContext)
fetchRequest.entity = entity
fetchRequest.predicate = (predicate != nil ? NSPredicate(format: predicate) : nil)
fetchRequest.sortDescriptors = sortDescriptors
Expand All @@ -110,48 +110,49 @@ public extension CDHelperEntity {
return fetchRequest
}

private static func _buildFindAllRequest(usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil) -> NSFetchRequest {
fileprivate static func _buildFindAllRequest(usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil) -> NSFetchRequest<Self> {
return self._buildFetchRequestUsingPredicate(nil, sortDescriptors: sortDescriptors, fetchLimit: nil)
}

private static func _buildFindOneRequest(predicate: String) -> NSFetchRequest {
fileprivate static func _buildFindOneRequest(_ predicate: String) -> NSFetchRequest<Self> {
return self._buildFetchRequestUsingPredicate(predicate, sortDescriptors: nil, fetchLimit: nil)
}

private static func _buildFindRequest(predicate: String, usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil, limit fetchLimit: Int! = nil) -> NSFetchRequest {
fileprivate static func _buildFindRequest(_ predicate: String, usingSortDescriptors sortDescriptors: [NSSortDescriptor]! = nil, limit fetchLimit: Int! = nil) -> NSFetchRequest<Self> {
return self._buildFetchRequestUsingPredicate(predicate, sortDescriptors: sortDescriptors, fetchLimit: fetchLimit)
}

private static func _fetchUsingFetchRequest(fetchRequest: NSFetchRequest) -> [Self] {
guard let results = try? Self.mainContext.executeFetchRequest(fetchRequest) as? [Self], let resultsArray = results.map({$0}) else {
fileprivate static func _fetchUsingFetchRequest(_ fetchRequest: NSFetchRequest<Self>) -> [Self] {
guard let results = try? Self.mainContext.fetch(fetchRequest) else {
print("CDHelper error: Cannot fetch results. Empty array has been returned.")
return []
}

return resultsArray
return results
}

private static func _asynchronouslyFetchUsingRequest(fetchRequest: NSFetchRequest, completion: Any) {
fileprivate static func _asynchronouslyFetchUsingRequest(_ fetchRequest: NSFetchRequest<Self>, completion: Any) {
let asyncRequest: NSAsynchronousFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { (result: NSAsynchronousFetchResult!) -> Void in
let result: [Self]? = (result.finalResult as? [Self])?.map({$0})

let result: [Self] = result.finalResult ?? []

if let completion = completion as? (([Self]) -> Void) {
completion(result != nil ? result! : [])
completion(result)
} else if let completion = completion as? ((Self?) -> Void) {
completion(result?.first)
completion(result.first ?? nil)
} else {
fatalError("CDHelper error: completion variable has a wrong type. Must be '([Self]) -> Void' or '(Self?) -> Void'.")
}
}

do {
try Self.mainContext.executeRequest(asyncRequest)
try Self.mainContext.execute(asyncRequest as NSPersistentStoreRequest)
} catch {
print("CDHelper error: Cannot fetch results. Empty array has been returned.")
}
}

private static func _saveContext() {
fileprivate static func _saveContext() {
if self.mainContext.hasChanges {
do {
try self.mainContext.save()
Expand All @@ -165,29 +166,16 @@ public extension CDHelperEntity {
// MARK: - CoreDataHelper main class
public final class CDHelper {

// MARK: Private class variables
private class var _sharedInstance: CDHelper {

struct Static {
static var instance: CDHelper?
static var token: dispatch_once_t = 0
}

dispatch_once(&Static.token) {
Static.instance = CDHelper()
}

return Static.instance!
}
fileprivate static var _sharedInstance: CDHelper = CDHelper()

// MARK: Public class variables
public class var mainContext: NSManagedObjectContext! { return self._sharedInstance._mainContext }

// MARK: Private instance variables
private var _mainContext: NSManagedObjectContext?
fileprivate var _mainContext: NSManagedObjectContext?

// MARK: Public class methods
public class func initializeWithMainContext(mainContext: NSManagedObjectContext) {
public class func initializeWithMainContext(_ mainContext: NSManagedObjectContext) {
self._sharedInstance._mainContext = mainContext
}
}
}

0 comments on commit 0140bbe

Please sign in to comment.