martijnwalraven / meteor-ios

Meteor iOS integrates native iOS apps with the Meteor platform (http://www.meteor.com) through DDP
MIT License
740 stars 79 forks source link

Booleans mapped to numbers on backend #38

Open justinmakaila opened 9 years ago

justinmakaila commented 9 years ago

I have a model:

class Update: MeteorCoreDataDocument {
    @NSManaged var createdAt: NSDate
    @NSManaged var endedAt: NSDate?
    @NSManaged var createdBy: String
    @NSManaged var isAvailable: Bool
    @NSManaged var update: Update?

    var isLive: Bool {
        return endedAt == nil
    }
}

When I create an instance of this model on the backend, it's done like so:

let newUpdate = NSEntityDescription.insertNewObjectForEntityForName("Update", inManagedObjectContext: managedObjectContext) as! Update
newUpdate.createdBy = Meteor.userID!
newUpdate.createdAt = NSDate()
newUpdate.isAvailable = true

saveManagedObjectContext()

In my test web app, I retrieve updates with this query:

Updates.find({
  createdBy: { $in: friends },
  isAvailable: true,
  update: null
}, { sort: { createdAt: -1 } }).fetch()

No updates will come back, because isAvailable is mapped to a 1 or 0, and is not picked up to be truthy or falsey by the mongo query.

Is there anyway to map this to a raw boolean value?

pozylon commented 9 years ago

I had the exact same problem, use "Transformable" and see: https://github.com/martijnwalraven/meteor-ios/issues/24

justinmakaila commented 9 years ago

Can you explain a bit?

Do I set the isAvailable property to a transformable in the .xcdatamodeld? If so, how do I associate a value transformer with it?

pozylon commented 9 years ago

I think you actually don't need to use my Transformer at all, it works if you just set it to Transformable and cast it:

read: let isPresent : Bool = object.isPresent as? Bool write: object.isPresent = isPresent (Bool)

pozylon commented 9 years ago

For NSValueTransformers: here you go http://nshipster.com/nsvaluetransformer/

justinmakaila commented 9 years ago

Well, the property is right on the client side, it’s not right on the web app due to the mapping, which leads to segmentation and weird issues. 

—Justin Makaila

On Fri, Jun 19, 2015 at 9:07 AM, pozylon notifications@github.com wrote:

I think you actually don't need to use my Transformer at all, it works if you just set it to Transformable and cast it: read: let isPresent : Bool = object.isPresent as? Bool write:

object.isPresent = isPresent (Bool)

Reply to this email directly or view it on GitHub: https://github.com/martijnwalraven/meteor-ios/issues/38#issuecomment-113509768