mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-22 01:19:42 +01:00
querying
This commit is contained in:
@@ -41,13 +41,22 @@ class HardcoreDataTests: XCTestCase {
|
||||
|
||||
func testExample() {
|
||||
|
||||
#if DEBUG
|
||||
let resetStoreOnMigrationFailure = true
|
||||
#else
|
||||
let resetStoreOnMigrationFailure = false
|
||||
#endif
|
||||
let stack = DataStack()
|
||||
HardcoreData.defaultStack = stack
|
||||
XCTAssertEqual(HardcoreData.defaultStack, stack, "HardcoreData.defaultStack == stack")
|
||||
|
||||
switch HardcoreData.defaultStack.addSQLiteStore(resetStoreOnMigrationFailure: resetStoreOnMigrationFailure) {
|
||||
switch stack.addSQLiteStore("Config1Store", configuration: "Config1", resetStoreOnMigrationFailure: true){
|
||||
|
||||
case .Failure(let error):
|
||||
NSException(
|
||||
name: "CoreDataMigrationException",
|
||||
reason: error.localizedDescription,
|
||||
userInfo: error.userInfo).raise()
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
switch stack.addSQLiteStore("Config2Store", configuration: "Config2", resetStoreOnMigrationFailure: true){
|
||||
|
||||
case .Failure(let error):
|
||||
NSException(
|
||||
@@ -61,27 +70,43 @@ class HardcoreDataTests: XCTestCase {
|
||||
|
||||
HardcoreData.performTransactionAndWait({ (transaction) -> () in
|
||||
|
||||
let obj = transaction.create(TestEntity1)
|
||||
obj.testEntityID = 1
|
||||
obj.testString = "lololol"
|
||||
obj.testNumber = 42
|
||||
obj.testDate = NSDate()
|
||||
|
||||
let obj1 = transaction.create(TestEntity1)
|
||||
obj1.testEntityID = 1
|
||||
obj1.testString = "lololol"
|
||||
obj1.testNumber = 42
|
||||
obj1.testDate = NSDate()
|
||||
|
||||
let obj2 = transaction.create(TestEntity2)
|
||||
obj2.testEntityID = 2
|
||||
obj2.testString = "hahaha"
|
||||
obj2.testNumber = 7
|
||||
obj2.testDate = NSDate()
|
||||
|
||||
transaction.commitAndWait()
|
||||
})
|
||||
|
||||
HardcoreData.performTransactionAndWait({ (transaction) -> () in
|
||||
|
||||
let obj = transaction.findAll(
|
||||
TestEntity1
|
||||
.WHERE("testEntityID", isEqualTo: 1)
|
||||
.SORTEDBY(.Ascending("testEntityID"), .Descending("testString")),
|
||||
customizeFetch: { (fetchRequest) -> () in
|
||||
let obj1 = transaction.fetchOne(
|
||||
TestEntity1.self,
|
||||
Where("testEntityID", isEqualTo: 1),
|
||||
SortedBy(.Ascending("testEntityID"), .Descending("testString")),
|
||||
CustomizeQuery { (fetchRequest) -> Void in
|
||||
|
||||
fetchRequest.includesPendingChanges = true
|
||||
}
|
||||
)
|
||||
NSLog("%@", obj ?? [])
|
||||
NSLog(">>>>> %@", obj1 ?? "nil")
|
||||
|
||||
let objs2 = transaction.fetchAll(
|
||||
TestEntity2.self,
|
||||
Where("testEntityID", isEqualTo: 2) && Where("testNumber", isEqualTo: 7),
|
||||
SortedBy(.Ascending("testEntityID"), .Descending("testString")),
|
||||
CustomizeQuery { (fetchRequest) -> () in
|
||||
|
||||
fetchRequest.includesPendingChanges = true
|
||||
}
|
||||
)
|
||||
NSLog(">>>>> %@", objs2 ?? "nil")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,20 @@
|
||||
<attribute name="testNumber" optional="YES" attributeType="Integer 32" defaultValueString="0" syncable="YES"/>
|
||||
<attribute name="testString" optional="YES" attributeType="String" syncable="YES"/>
|
||||
</entity>
|
||||
<entity name="TestEntity2" representedClassName="HardcoreDataTests.TestEntity2" syncable="YES">
|
||||
<attribute name="testDate" optional="YES" attributeType="Date" syncable="YES"/>
|
||||
<attribute name="testEntityID" attributeType="Integer 64" syncable="YES"/>
|
||||
<attribute name="testNumber" optional="YES" attributeType="Integer 32" defaultValueString="0" syncable="YES"/>
|
||||
<attribute name="testString" optional="YES" attributeType="String" syncable="YES"/>
|
||||
</entity>
|
||||
<configuration name="Config1">
|
||||
<memberEntity name="TestEntity1"/>
|
||||
</configuration>
|
||||
<configuration name="Config2">
|
||||
<memberEntity name="TestEntity2"/>
|
||||
</configuration>
|
||||
<elements>
|
||||
<element name="TestEntity1" positionX="-63" positionY="-18" width="128" height="105"/>
|
||||
<element name="TestEntity2" positionX="-63" positionY="9" width="128" height="103"/>
|
||||
</elements>
|
||||
</model>
|
||||
36
HardcoreDataTests/TestEntity2.swift
Normal file
36
HardcoreDataTests/TestEntity2.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// TestEntity1.swift
|
||||
// HardcoreData
|
||||
//
|
||||
// Copyright (c) 2014 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
|
||||
import CoreData
|
||||
|
||||
class TestEntity2: NSManagedObject {
|
||||
|
||||
@NSManaged var testEntityID: NSNumber?
|
||||
@NSManaged var testString: String?
|
||||
@NSManaged var testNumber: NSNumber?
|
||||
@NSManaged var testDate: NSDate?
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user