Created
June 4, 2020 03:40
-
-
Save League2EB/5f1f0091f68895a25abed9790984e0c6 to your computer and use it in GitHub Desktop.
RealmSwift + Rx
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
func delete<T>(type: T.Type, code: String) -> Observable<()> where T: Object { | |
return Observable.create { observer in | |
do { | |
let realm = try Realm() | |
guard let object = realm.objects(type).filter(code).first else { return Disposables.create() } | |
realm.delete(object) | |
observer.onNext(()) | |
observer.onCompleted() | |
} catch let error { | |
observer.onError(error) | |
} | |
return Disposables.create() | |
} | |
} |
thanks for your questions.
I think the first thing to do is to specify a T: Object
and prepare a deletable method.
Next I thought it would be good to create a method if you want to filter by items other than keys
example)
static func delete<T>(element: T) -> Observable<()> where T: Object {
return Observable.create { observer in
do {
let realm = try Realm()
try realm.write {
realm.delete(element)
observer.onNext(())
observer.onCompleted()
}
} catch let error {
observer.onError(error)
}
return Disposables.create()
}
}
static func deleteBy<T>(element: T, code: String) -> Observable<()> where T : Object {
return Realm.query(type: T.self, formattedString: NSPredicate(format: "code == %@", argumentArray: [code]))
.flatMap { Realm.delete(element: $0) }
}
(* I have not actually operated it)
Thank you!
@Slowhand0309
Thx all !
台日友好
👍
@League2EB
I fixed it so that it compiles!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Slowhand0309