Created
October 4, 2017 13:09
-
-
Save NikhilManapure/76f1469e57bda62c8194c1102f3a3da4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import RealmSwift | |
class Item: Object { | |
dynamic var name: String = "" | |
dynamic var date: Date = Date() | |
static var all: [Item] { | |
get { | |
do { | |
let realm = try Realm() | |
return Array(realm.objects(Item.self).sorted(byKeyPath: "date", ascending: false)) | |
} catch { | |
print("Item.all - Something went wrong !!!") | |
} | |
return [] | |
} | |
} | |
func update(name: String? = nil) -> Bool { | |
do { | |
let realm = try Realm() | |
try realm.write() { | |
if let name = name { | |
if self.name != name { | |
self.name = name | |
} | |
} | |
realm.add(self, update: true) | |
} | |
image?.saveToLocal(withName: self.imageName) | |
return true | |
} catch { | |
print("Item.update - Something went wrong !!!") | |
} | |
return false | |
} | |
func delete() { | |
do { | |
let realm = try Realm() | |
try realm.write() { | |
realm.delete(self) | |
} | |
} catch { | |
print("Item.delete - Something went wrong !!!") | |
} | |
} | |
// override static func ignoredProperties() -> [String] { | |
// return [""] | |
// } | |
// override class func primaryKey() -> String? { | |
// return "" | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment