Skip to content

Importing UniqueObjects and deleting with RxCoreStore #2

Description

@tosbaha

Hi,
I am converting my previous CoreStore functions to RxSwift version. My previous code was something like this.

Storage.stack.perform(asynchronous: { transaction in
    let imported = try! transaction.importUniqueObjects(Into(KargoCD.self), sourceArray: kargos)
    transaction.deleteAll(From<KargoCD>(), Where<KargoCD>("NOT (SELF IN %@)", imported))
}, success: { done in
    logger.info("DoneStore")
}, failure: { error in
    logger.error("MyError CoreStore: \(error)")
})

It is basically importing all objects from JSON and deletes non existing objects from CoreStore. Since I don't like using try! I tried to change code to something like below with RxCoreStore

return Storage.stack.rx.perform(asynchronous: { transaction -> [KargoCD] in
    
    do {
        let imported = try transaction.importUniqueObjects(Into(KargoCD.self), sourceArray: kargos)
        transaction.deleteAll(From<KargoCD>(), Where<KargoCD>("NOT (SELF IN %@)", imported))
        return imported
    } catch  {
        return []
    }
})

I know there is rx for DataStack but I can't use transaction.rx so again I had to use transaction directly. Here also I return empty array for catching error which seems hacky. What is the safest way to importUniqueObjects with Rx way. I also come up with below code but I don't know whether it is safe or not because delete transaction and importing is happening in different transactions.

            }.flatMap { kargos -> Observable<[KargoCD]> in
                let imported = Storage.stack.rx.importUniqueObjects(Into(KargoCD.self), sourceArray: kargos)
                return imported
            }.flatMap({ imported -> Observable<[KargoCD]> in
                return Storage.stack.rx.perform(asynchronous: { transaction in
                    transaction.deleteAll(From<KargoCD>(), Where<KargoCD>("NOT (SELF IN %@)", imported))
                    return imported
                })
            })

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions