Last active
July 19, 2017 15:30
-
-
Save ennioma/509ea78a611bde3330c810e0e3919df2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension RealmStorageContext { | |
func fetch<T: Storable>(_ model: T.Type, predicate: NSPredicate? = nil, sorted: Sorted? = nil, completion: (([T]) -> ())) { | |
var objects = self.realm?.objects(model as! Object.Type) | |
if let predicate = predicate { | |
objects = objects?.filter(predicate) | |
} | |
if let sorted = sorted { | |
objects = objects?.sorted(byKeyPath: sorted.key, ascending: sorted.ascending) | |
} | |
completion(objects.flatMap { $0 as? T }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment