Update Request interface making it more Swifty

This commit is contained in:
Pedro Piñera Buendía 2016-10-07 17:01:39 +02:00
parent 4a2d8e2d08
commit ec5fb59e92
7 changed files with 19 additions and 18 deletions

View File

@ -15,7 +15,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2334C4C01D7DDA1400ACBC3A"
BuildableName = "SugarRecord.framework"
BuildableName = "SugarRecordRealm.framework"
BlueprintName = "iOSRealm"
ReferencedContainer = "container:Carthage.xcodeproj">
</BuildableReference>
@ -46,7 +46,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2334C4C01D7DDA1400ACBC3A"
BuildableName = "SugarRecord.framework"
BuildableName = "SugarRecordRealm.framework"
BlueprintName = "iOSRealm"
ReferencedContainer = "container:Carthage.xcodeproj">
</BuildableReference>
@ -64,7 +64,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2334C4C01D7DDA1400ACBC3A"
BuildableName = "SugarRecord.framework"
BuildableName = "SugarRecordRealm.framework"
BlueprintName = "iOSRealm"
ReferencedContainer = "container:Carthage.xcodeproj">
</BuildableReference>

View File

@ -97,7 +97,7 @@ class CoreDataBasicView: UIViewController, UITableViewDelegate, UITableViewDataS
if editingStyle == UITableViewCellEditingStyle.delete {
let name = entities[(indexPath as NSIndexPath).row].name
try! db.operation({ (context, save) -> Void in
guard let obj = try! context.request(BasicObject.self).filteredWith("name", equalTo: name).fetch().first else { return }
guard let obj = try! context.request(BasicObject.self).filtered(with: "name", equalTo: name).fetch().first else { return }
_ = try? context.remove(obj)
save()
})

View File

@ -96,7 +96,7 @@ class RealmBasicView: UIViewController, UITableViewDelegate, UITableViewDataSour
if editingStyle == UITableViewCellEditingStyle.delete {
let name = entities[(indexPath as NSIndexPath).row].name
try! db.operation({ (context, save) -> Void in
guard let obj = try! context.request(RealmBasicObject.self).filteredWith("name", equalTo: name).fetch().first else { return }
guard let obj = try! context.request(RealmBasicObject.self).filtered(with: "name", equalTo: name).fetch().first else { return }
_ = try? context.remove(obj)
save()
})

View File

@ -35,32 +35,32 @@ public struct FetchRequest<T: Entity>: Equatable {
// MARK: - Public Builder Methods
public func filteredWith(predicate: NSPredicate) -> FetchRequest<T> {
public func filtered(with predicate: NSPredicate) -> FetchRequest<T> {
return self
.request(withPredicate: predicate)
}
public func filteredWith(_ key: String, equalTo value: String) -> FetchRequest<T> {
public func filtered(with key: String, equalTo value: String) -> FetchRequest<T> {
return self
.request(withPredicate: NSPredicate(format: "\(key) == %@", value))
}
public func sortedWith(sortDescriptor: NSSortDescriptor) -> FetchRequest<T> {
public func sorted(with sortDescriptor: NSSortDescriptor) -> FetchRequest<T> {
return self
.request(withSortDescriptor: sortDescriptor)
}
public func sortedWith(_ key: String?, ascending: Bool, comparator cmptr: @escaping Comparator) -> FetchRequest<T> {
public func sorted(with key: String?, ascending: Bool, comparator cmptr: @escaping Comparator) -> FetchRequest<T> {
return self
.request(withSortDescriptor: NSSortDescriptor(key: key, ascending: ascending, comparator: cmptr))
}
public func sortedWith(_ key: String?, ascending: Bool) -> FetchRequest<T> {
public func sorted(with key: String?, ascending: Bool) -> FetchRequest<T> {
return self
.request(withSortDescriptor: NSSortDescriptor(key: key, ascending: ascending))
}
public func sortedWith(_ key: String?, ascending: Bool, selector: Selector) -> FetchRequest<T> {
public func sorted(with key: String?, ascending: Bool, selector: Selector) -> FetchRequest<T> {
return self
.request(withSortDescriptor: NSSortDescriptor(key: key, ascending: ascending, selector: selector))
}

View File

@ -19,7 +19,7 @@ class CoreDataObservableTests: QuickSpec {
let model = CoreDataObjectModel.merged([bundle])
storage = try! CoreDataDefaultStorage(store: store, model: model)
_ = try? storage.removeStore()
request = FetchRequest<Track>().sortedWith("name", ascending: true)
request = FetchRequest<Track>().sorted(with: "name", ascending: true)
let context: NSManagedObjectContext = storage.mainContext as! NSManagedObjectContext
subject = CoreDataObservable(request: request, context: context)
context.perform({

View File

@ -141,7 +141,8 @@ class CoreDataDefaultStorageTests: QuickSpec {
var observable: CoreDataObservable<Track>!
beforeEach {
request = FetchRequest<Track>().filteredWith("name", equalTo: "test").sortedWith("name", ascending: true)
request = FetchRequest<Track>().filtered(with: "name", equalTo: "test")
.sorted(with: "name", ascending: true)
observable = subject.observable(request: request) as! CoreDataObservable<Track>
}

View File

@ -14,33 +14,33 @@ class RequestTests: QuickSpec {
it("-filteredWithPredicate") {
let predicate: NSPredicate = NSPredicate(format: "name == TEST")
let request: FetchRequest<Issue> = FetchRequest(testRealm()).filteredWith(predicate: predicate)
let request: FetchRequest<Issue> = FetchRequest(testRealm()).filtered(with: predicate)
expect(request.predicate) == predicate
}
it("-filteredWith(key:value:)") {
let predicate: NSPredicate = NSPredicate(format: "name == %@", "TEST")
let request: FetchRequest<Issue> = FetchRequest(testRealm()).filteredWith("name", equalTo: "TEST")
let request: FetchRequest<Issue> = FetchRequest(testRealm()).filtered(with: "name", equalTo: "TEST")
expect(request.predicate) == predicate
}
it("-sortedWith(key:ascending:comparator)") {
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true, comparator: { _ in return ComparisonResult.orderedSame})
let request: FetchRequest<Issue> = FetchRequest(testRealm()).sortedWith("name", ascending: true, comparator: {_ in return ComparisonResult.orderedSame})
let request: FetchRequest<Issue> = FetchRequest(testRealm()).sorted(with: "name", ascending: true, comparator: {_ in return ComparisonResult.orderedSame})
expect(descriptor.key) == request.sortDescriptor?.key
expect(descriptor.ascending) == request.sortDescriptor?.ascending
}
it("-sortedWith(key:ascending)") {
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true)
let request: FetchRequest<Issue> = FetchRequest(testRealm()).sortedWith("name", ascending: true)
let request: FetchRequest<Issue> = FetchRequest(testRealm()).sorted(with: "name", ascending: true)
expect(descriptor.key) == request.sortDescriptor?.key
expect(descriptor.ascending) == request.sortDescriptor?.ascending
}
it("sortedWith(key:ascending:selector)") {
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true, selector: Selector("selector"))
let request: FetchRequest<Issue> = FetchRequest(testRealm()).sortedWith("name", ascending: true, selector: Selector("selector"))
let request: FetchRequest<Issue> = FetchRequest(testRealm()).sorted(with: "name", ascending: true, selector: Selector("selector"))
expect(descriptor.key) == request.sortDescriptor?.key
expect(descriptor.ascending) == request.sortDescriptor?.ascending
expect(descriptor.selector) == request.sortDescriptor?.selector