Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coredata: how to call sync method for attribute<CDModalName> #52

Open
iamitksharma opened this issue Sep 11, 2018 · 2 comments
Open

Coredata: how to call sync method for attribute<CDModalName> #52

iamitksharma opened this issue Sep 11, 2018 · 2 comments

Comments

@iamitksharma
Copy link

For Model like CDMessage,

Why func sync not called when I try to save CDMessage data in core data.

CDConversation contained CDMessage, for reference I am sharing response below:
{ id : 23,
message : {
id : 123,
text : "hello"
}
}

In CDModalName+ext.swift :

extension CDConversation {
public static var id: Attribute<String> { return Attribute("id")}

     public static var last_message: Attribute<CDMessage> { return Attribute("message")}  
}

extension Conversation: CoreDataRepresentable {

public typealias CoreDataType = CDConversation

func sync(in context: NSManagedObjectContext) -> Observable<CDConversation> {
    let syncSelf = context.rx.sync(entity: self, update: update)
    let syncLast_message = last_message!.sync(in: context).map(Optional.init)
    return Observable.zip(syncSelf, syncLast_message, resultSelector: CDConversation.synced)
}

public func update(entity: CDConversation) {
    entity.id = Int16(id)

}
}

@hackolein
Copy link

I have the same issue, the func sync(in context: NSManagedObjectContext) in the Core Data class extensions never called. The default sync function in the protocol extension is always called.

@hackolein
Copy link

I solved the problem, you have to add the function to the protocol CoreDataRepresentable
func sync(in context: NSManagedObjectContext) -> Observable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants