Search code examples
iosswiftparse-platformsubclassingpfquery

Casting failed maybe related to Pointer


SCENARIO

the scenario consists in 2 classes Operations and Places

these 2 classes are related as described below

Operation{
   storedAt : Date
   type -> OperationType (Pointer)
   place -> Places (Pointer)
}

Places {
   title : String
   type -> OperationType (Pointer)
   location -> PFGeoPoint
} 

what I would to do It's simply to retrieve the places near to user. For this I used:

func getNearPlaces(location: CLLocation){
    if location.isZero() {
        return
    }

    var placeQuery = PFQuery(className: "Places")
    println("getNearPlaces location  \(location)")
    placeQuery.whereKey("location", nearGeoPoint: PFGeoPoint(location:location), withinKilometers: 2)
    placeQuery.whereKey("type", equalTo: detailItem.type)
    placeQuery.limit = 15

    placeQuery.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
        if error != nil {
            println("error: \(error)")
            return
        }

        println("results: \(objects?.count)")
        if let objs = objects as? [PFObject] {
            println("casting to [PFObject] ok")
        }

        if let objs = objects as? [Places] {
            println("casting to [Places] ok")
                self.places = objs
        }
        self.updateLocationOnMap()
    }
}

where detailItem is a subclass of Operations

Now the point! If Operations class has NO place pointer column valorized on class for all rows all works fine.

results: Optional(3)
casting to [PFObject] ok
casting to [Places] ok

Next I assigned detailItem.place with one of [Places] value and I continue to use app. And all it's ok.

when I start again the app and detailItem.place is valorized, this is the output I have:

results: Optional(3)
casting to [PFObject] ok

casting to [Places] failed

could somebody explain me why? is it something relates di parse or swift? is there something I make wrong?

I add some detail more:

Accordingly with how is defined on Parse.com Doc:

class Places:PFObject,PFSubclassing{

override class func initialize() {
    struct Static {
        static var onceToken : dispatch_once_t = 0;
    }
    dispatch_once(&Static.onceToken) {
        self.registerSubclass()
    }
}

static func parseClassName() -> String {
    return "Places"
}

@NSManaged var title:String
@NSManaged var type:OperationsType
@NSManaged var location:PFGeoPoint

}

below the output of the objects retrieved from PFQuery described above:

[<Places: 0x7fb94dc9e9d0, objectId: yFDNHinajo, localId: (null)> 
{
    location = "<PFGeoPoint: 0x7fb94d587ad0, latitude: 95.497511, longitude: 19.224867>";
    title = tamoilled;
    type = "<OperationsType: 0x7fb94844a2f0, objectId: 2IPOWNo1lM>";
}, 
<Places: 0x7fb94a84b2f0, objectId: zlh3CMVu0t, localId: (null)> 
{
    location = "<PFGeoPoint: 0x7fb94dc63f20, latitude: 95.498064, longitude: 19.226219>";
    title = "new Place";
    type = "<OperationsType: 0x7fb94844a2f0, objectId: 2IPOWNo1lM>";
}, 
<Places: 0x7fb94dc9f8f0, objectId: ymY7SlA3Is, localId: (null)> 
{
    location = "<PFGeoPoint: 0x7fb94dce4e70, latitude: 95.510635, longitude: 19.217392>";
    title = "Agip 2";
   type = "<OperationsType: 0x7fb94844a2f0, objectId: 2IPOWNo1lM>";
}]

Parse iOS sdk 1.7.2, Xcode 6.3.1

on didFinishLaunchingWithOptions

ParseCrashReporting.enable();
Parse.enableLocalDatastore()
Parse.setApplicationId("...", clientKey:"...");

UPDATE

I replaced

println("results: \(objects?.count)")
if let objs = objects as? [PFObject] {
    println("casting to [PFObjects] ok \(objs)" )
}

with

self.places = []
for result in objects! {
    println("object \(result)")
    if let place  = result as? Places {
        println("place ok")
        self.places.append(place)
    }
}

and the result is that the Places object corresponding to detailItem.place is no casted to a Places even if It seems to be correct! (be focus on the first dictionary. it's not followed by 'place ok')

object <Places: 0x7ff5a0f64be0, objectId: yFDNHinajo, localId: (null)> {
location = "<PFGeoPoint: 0x7ff5a312c720, latitude: 95.497511, longitude: 19.224867>";
title = tamoilled;
type = "<OperationsType: 0x7ff5a0edd520, objectId: 2IPOWNo1lM>";
}
object <Places: 0x7ff5a0fbcdc0, objectId: zlh3CMVu0t, localId: (null)> {
location = "<PFGeoPoint: 0x7ff5a3121f30, latitude: 95.498064, longitude: 19.226219>";
title = "new Place";
type = "<OperationsType: 0x7ff5a0edd520, objectId: 2IPOWNo1lM>";
}  
place ok
object <Places: 0x7ff5a3724320, objectId: ymY7SlA3Is, localId: (null)> {
location = "<PFGeoPoint: 0x7ff5a31b8450, latitude: 95.510635, longitude: 19.217392>";
title = "Agip 2";
type = "<OperationsType: 0x7ff5a0edd520, objectId: 2IPOWNo1lM>";
}
place ok

UPDATE 2

I tried to execute the same query on start app. no errors and cast works fine I have no more resources right now. I think it could be a bug coming from Parse.com framework. Or something I don't know on my scenario (that should be very simple)


Solution

  • SOLVED

    with the new Parse's update, they suggest to add explicit

    Places.registerSubclass()
    

    before

    Parse.setApplicationId()