mentrena / SyncKit

Automatic CloudKit synchronization
https://mentrena.github.io/SyncKit/
MIT License
507 stars 59 forks source link

Is there any way to set primary key Int? #116

Closed sambong0113 closed 4 years ago

sambong0113 commented 4 years ago

I'm using primary key to id type of Int64. If that it return nil because of failing String converting.

Is there any way to use primary key type of Int?

mentrena commented 4 years ago

Not as such, but you could just do

@NSManaged public var identifier: Int? // The Int identifier you're using

@objc public var identifierAsString: String? { // A computed property to transform Int<->String
    get {
        return identifier != nil ? String(identifier!) : nil
    }
    set {
        identifier = newValue != nil ? Int(newValue!) : nil
    }
}

public static func primaryKey() -> String {
        return "identifierAsString"
    }
sambong0113 commented 4 years ago

I just try that code, but it returns nil error in that function.

스크린샷 2020-03-28 오후 4 03 27

The originalObject is nil because of NSPredicate fail.

Is there any possible way to fix it?

mentrena commented 4 years ago

Ah right, can't fetch on a computed property :/

I think it would be possible to support Int identifiers as well, but if you want to use the library in the meantime you would have to add a String type identifier to your objects.