allow fetching from HCD

This commit is contained in:
John Rommel Estropia
2015-02-21 21:48:08 +09:00
parent 061a863235
commit f6ced13577
10 changed files with 137 additions and 32 deletions

View File

@@ -12,6 +12,8 @@
2F03A54D19C5C872005002A5 /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F03A54C19C5C872005002A5 /* CoreData.framework */; };
2F291E2719C6D3CF007AF63F /* HardcoreData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F291E2619C6D3CF007AF63F /* HardcoreData.swift */; };
B57078B01A50392D007E33F2 /* FetchClause.swift in Sources */ = {isa = PBXBuildFile; fileRef = B57078AF1A50392D007E33F2 /* FetchClause.swift */; };
B582DF821A98B0E7003F09C6 /* HardcoreData+Querying.swift in Sources */ = {isa = PBXBuildFile; fileRef = B582DF811A98B0E7003F09C6 /* HardcoreData+Querying.swift */; };
B582DF861A98B11B003F09C6 /* HardcoreData+Transaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B582DF851A98B11B003F09C6 /* HardcoreData+Transaction.swift */; };
B5CFD36E1A0775F000B7885F /* SaveResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CFD36D1A0775F000B7885F /* SaveResult.swift */; };
B5CFF23E19FD1D1C00D6DFC4 /* NSManagedObjectContext+HardcoreData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CFF23D19FD1D1C00D6DFC4 /* NSManagedObjectContext+HardcoreData.swift */; };
B5CFF24019FD383100D6DFC4 /* DataTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CFF23F19FD383100D6DFC4 /* DataTransaction.swift */; };
@@ -72,6 +74,8 @@
2F03A54C19C5C872005002A5 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
2F291E2619C6D3CF007AF63F /* HardcoreData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = HardcoreData.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
B57078AF1A50392D007E33F2 /* FetchClause.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchClause.swift; sourceTree = "<group>"; };
B582DF811A98B0E7003F09C6 /* HardcoreData+Querying.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HardcoreData+Querying.swift"; sourceTree = "<group>"; };
B582DF851A98B11B003F09C6 /* HardcoreData+Transaction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HardcoreData+Transaction.swift"; sourceTree = "<group>"; };
B5CFD36D1A0775F000B7885F /* SaveResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SaveResult.swift; sourceTree = "<group>"; };
B5CFF23D19FD1D1C00D6DFC4 /* NSManagedObjectContext+HardcoreData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+HardcoreData.swift"; sourceTree = "<group>"; };
B5CFF23F19FD383100D6DFC4 /* DataTransaction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataTransaction.swift; sourceTree = "<group>"; };
@@ -204,6 +208,7 @@
isa = PBXGroup;
children = (
B5CFF23F19FD383100D6DFC4 /* DataTransaction.swift */,
B582DF851A98B11B003F09C6 /* HardcoreData+Transaction.swift */,
B5D022651A90CD340070CA63 /* DataStack+Transaction.swift */,
B5CFD36D1A0775F000B7885F /* SaveResult.swift */,
);
@@ -251,6 +256,7 @@
B5E126541A7DCE1400AD8B39 /* Where.swift */,
B5E126561A7DCE5900AD8B39 /* SortedBy.swift */,
B5F409F01A8B27A600A228EA /* CustomizeQuery.swift */,
B582DF811A98B0E7003F09C6 /* HardcoreData+Querying.swift */,
B5F409EC1A8B200700A228EA /* NSManagedObjectContext+Querying.swift */,
B5F409EE1A8B243D00A228EA /* DataTransaction+Querying.swift */,
);
@@ -401,6 +407,7 @@
B5D399F519FCF4E0000E91BB /* NSPersistentStoreCoordinator+HardcoreData.swift in Sources */,
B5CFD36E1A0775F000B7885F /* SaveResult.swift in Sources */,
B5D022661A90CD340070CA63 /* DataStack+Transaction.swift in Sources */,
B582DF861A98B11B003F09C6 /* HardcoreData+Transaction.swift in Sources */,
B5D1E22C19FA9FBC003B2874 /* NSError+HardcoreData.swift in Sources */,
B5CFF23E19FD1D1C00D6DFC4 /* NSManagedObjectContext+HardcoreData.swift in Sources */,
B5E126571A7DCE5900AD8B39 /* SortedBy.swift in Sources */,
@@ -409,6 +416,7 @@
B5F409E91A8B11CE00A228EA /* HardcoreDataLogger.swift in Sources */,
B5D8081A1A3495BD00A44484 /* NSObject+HardcoreData.swift in Sources */,
B5E209E01A0726460089C9D4 /* NSManagedObject+HardcoreData.swift in Sources */,
B582DF821A98B0E7003F09C6 /* HardcoreData+Querying.swift in Sources */,
B5D1E22A19FA9E63003B2874 /* PersistentStoreResult.swift in Sources */,
B5F409F11A8B27A600A228EA /* CustomizeQuery.swift in Sources */,
B5F409EB1A8B199600A228EA /* DefaultLogger.swift in Sources */,

View File

@@ -10,7 +10,7 @@ import Foundation
import CoreData
// MARK: - DataStack+Transaction
// MARK: - DataStack
extension DataStack {

View File

@@ -254,7 +254,7 @@ public class DataStack: NSObject {
// MARK: Internal
public let mainContext: NSManagedObjectContext
internal let mainContext: NSManagedObjectContext
internal let transactionQueue: GCDQueue;

View File

@@ -27,10 +27,12 @@ import Foundation
import CoreData
// MARK: - DataTransaction+Querying
// MARK: - DataTransaction
extension DataTransaction {
// MARK: Public
public func fetchOne<T: NSManagedObject>(entity: T.Type, _ queryClauses: FetchClause...) -> T? {
return self.context.fetchOne(entity, queryClauses)

View File

@@ -0,0 +1,63 @@
//
// HardcoreData+Querying.swift
// HardcoreData
//
// Copyright (c) 2015 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
// MARK: - HardcoreData
extension HardcoreData {
// MARK: Public
public static func fetchOne<T: NSManagedObject>(entity: T.Type, _ queryClauses: FetchClause...) -> T? {
return self.defaultStack.mainContext.fetchOne(entity, queryClauses)
}
public static func fetchOne<T: NSManagedObject>(entity: T.Type, _ queryClauses: [FetchClause]) -> T? {
return self.defaultStack.mainContext.fetchOne(entity, queryClauses)
}
public static func fetchAll<T: NSManagedObject>(entity: T.Type, _ queryClauses: FetchClause...) -> [T]? {
return self.defaultStack.mainContext.fetchAll(entity, queryClauses)
}
public static func fetchAll<T: NSManagedObject>(entity: T.Type, _ queryClauses: [FetchClause]) -> [T]? {
return self.defaultStack.mainContext.fetchAll(entity, queryClauses)
}
public static func queryCount<T: NSManagedObject>(entity: T.Type, _ queryClauses: FetchClause...) -> Int {
return self.defaultStack.mainContext.queryCount(entity, queryClauses)
}
public static func queryCount<T: NSManagedObject>(entity: T.Type, _ queryClauses: [FetchClause]) -> Int {
return self.defaultStack.mainContext.queryCount(entity, queryClauses)
}
}

View File

@@ -0,0 +1,55 @@
//
// HardcoreData+Transaction.swift
// HardcoreData
//
// Copyright (c) 2015 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
// MARK: - HardcoreData
extension HardcoreData {
// MARK: Public
/**
Using the defaultStack, begins a transaction asynchronously where NSManagedObject creates, updates, and deletes can be made.
:param: closure the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent NSManagedObjectContext.
*/
public static func performTransaction(closure: (transaction: DataTransaction) -> Void) {
self.defaultStack.performTransaction(closure)
}
/**
Using the defaultStack, begins a transaction asynchronously where NSManagedObject creates, updates, and deletes can be made.
:param: closure the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent NSManagedObjectContext.
:returns: a SaveResult value indicating success or failure, or nil if the transaction was not comitted synchronously
*/
public static func performTransactionAndWait(closure: (transaction: DataTransaction) -> Void) -> SaveResult? {
return self.defaultStack.performTransactionAndWait(closure)
}
}

View File

@@ -114,28 +114,3 @@ public struct HardcoreData {
private static var defaultStackInstance: DataStack?
}
extension HardcoreData {
/**
Using the defaultStack, begins a transaction asynchronously where NSManagedObject creates, updates, and deletes can be made.
:param: closure the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent NSManagedObjectContext.
*/
public static func performTransaction(closure: (transaction: DataTransaction) -> Void) {
self.defaultStack.performTransaction(closure)
}
/**
Using the defaultStack, begins a transaction asynchronously where NSManagedObject creates, updates, and deletes can be made.
:param: closure the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent NSManagedObjectContext.
:returns: a SaveResult value indicating success or failure, or nil if the transaction was not comitted synchronously
*/
public static func performTransactionAndWait(closure: (transaction: DataTransaction) -> Void) -> SaveResult? {
return self.defaultStack.performTransactionAndWait(closure)
}
}

View File

@@ -27,10 +27,12 @@ import Foundation
import CoreData
// MARK: - NSManagedObjectContext+Querying
// MARK: - NSManagedObjectContext
extension NSManagedObjectContext {
// MARK: Public
public func fetchOne<T: NSManagedObject>(entity: T.Type, _ queryClauses: FetchClause...) -> T? {
return self.fetchOne(entity, queryClauses)

View File

@@ -63,9 +63,9 @@ public struct Where: FetchClause {
self.init(NSPredicate(value: value))
}
public init(_ format: String, _ args: CVarArgType...) {
public init(_ format: String, _ args: NSObject...) {
self.init(NSPredicate(format: format, arguments: getVaList(args)))
self.init(NSPredicate(format: format, argumentArray: args))
}
public init(_ format: String, argumentArray: [AnyObject]?) {

View File

@@ -106,7 +106,7 @@ class HardcoreDataTests: XCTestCase {
let objs2 = transaction.fetchAll(
TestEntity2.self,
Where("testNumber", isEqualTo: 100) || Where("testNumber", isEqualTo: 90),
Where("testNumber", isEqualTo: 100) || Where("%K == %@", "testNumber", 90),
SortedBy(.Ascending("testEntityID"), .Descending("testString")),
CustomizeQuery { (fetchRequest) -> Void in