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

Cache with RealmSwift #36

Closed
arden opened this issue Apr 28, 2016 · 9 comments
Closed

Cache with RealmSwift #36

arden opened this issue Apr 28, 2016 · 9 comments

Comments

@arden
Copy link

arden commented Apr 28, 2016

when i'm use cache and RealmSwift,for example:

import Foundation
import RealmSwift
import ObjectMapper
import Cache

class Session: Object, Mappable {
dynamic var token = ""
dynamic var ttl: Double = 0

required convenience init?(_ map: Map) {
    self.init()
}

override static func primaryKey() -> String? {
    return "token"
}

func mapping(map: Map) {
    token               <- map["token"]
    ttl                 <- map["ttl"]
}

}

extension Session: Cachable {

typealias CacheType = Session

static func decode(data: NSData) -> CacheType? {
    Log.debug("decode")
    var object: CacheType?

    do {
        object = try DefaultCacheConverter<CacheType>().decode(data)
        Log.debug("object: \(object)")
    } catch {}

    return object
}

func encode() -> NSData? {
    var data: NSData?
    Log.debug("encode")
    do {
        data = try DefaultCacheConverter<CacheType>().encode(self)
    } catch {}

    return data
}

}

    let session = Session()
    session.token = "aaa"
    session.ttl = 20
    syncCache.add("tujiaoSession", object: session, expiry: .Date(NSDate().dateByAddingTimeInterval(100000)))

    let s:Session? = syncCache.object("tujiaoSession")
    Log.debug("ssssss: \(s)")

there have some errors for decode method.

@vadymmarkov
Copy link
Contributor

@arden Could you please specify what kind of errors do you get?

@arden
Copy link
Author

arden commented Apr 28, 2016

this error is because get cache data from Cache while have no data with given cache name.

@zenangst
Copy link
Contributor

@arden When I started reading this issue, it was interesting but seeing as you are trying to cache an actual Realm object, you now have my full attention. Mind sharing some more information about what you are doing here, just to ease my curiosity. Think about the cats...

Also, to help with debugging, it would help a lot if you pasted some of the error logs that you get.

@arden
Copy link
Author

arden commented Apr 28, 2016

OK,i'm use the Cache in my project, but found some errors, so i have removed the Cache from my project.
Later,i'm will to test again, and paste the errors to this topic.

@RamonGilabert
Copy link
Contributor

Why would you want to cache a Realm object, @arden? That would be like using Core Data to save Realm objects in a sense, does it make any sense?

@arden
Copy link
Author

arden commented Apr 28, 2016

@RamonGilabert Because i'm hope use only one Model class instead of serveral Model classes, that they are look the same.

@vadymmarkov
Copy link
Contributor

@arden DefaultCacheConverter is experimental helper, it's better to implement Cachable protocol in some other way, for example you could create a dictionary and cache it, then instantiate your object from this dictionary since you have Mappable.

@arden
Copy link
Author

arden commented Apr 28, 2016

@vadymmarkov thanks, i will try again use your proposal。

@zenangst
Copy link
Contributor

I'll close this issue for lack of activity, feel free to reopen if you have further issues.

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

4 participants