how to iterate the result from AGSGDBFeatureTable.queryResultsWithParameters

3137
4
Jump to solution
10-01-2015 02:33 PM
MeiWei
by
New Contributor

I got exception to access the results from queryResultsWithParameter, can someone please help?

let localFeatureTable = geodatabase.featureTables()[1] as! AGSGDBFeatureTable

localFeatureTable.queryResultsWithParameters(query , completion: { (tmpResult, error ) -> Void in

               

                print("error is \(error); result count is \(tmpResult.count)")

                let results = tmpResult as! [AGSFeature]

                for feature in results  {

                    print("feature: \(feature)")               <--- this line throws EXC_BAD_ACCESS

                }

0 Kudos
1 Solution

Accepted Solutions
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

Hi Mei Wei,

I think the way you using the new Swift2 do...try...catch syntax is not correct and also I am not really sure the way that you load the bundle may has some issue.

I do based on an offlineRouting sample and add the method queryResultsWithParameters. It works fine and return 310 results. I would suggest to instantiate the AGSGDBGeodatabase & AGSGDBFeatureTable after the viewController

    var localGDB: AGSGDBGeodatabase!
    var localFeatureTable: AGSGDBFeatureTable!

Then use this format to handle the error based on The Swift Programming Language (Swift 2.1): Error Handling

do {

try expression

statements

} catch pattern 1 {

statements

} catch pattern 2 where condition {

statements

}

And I tested with this code and it works. Hope this can help.

        if let gdbPath = NSBundle.mainBundle().pathForResource("RuntimeSanFrancisco", ofType: "geodatabase"){
        
            do{
               try self.localGDB = AGSGDBGeodatabase(path: gdbPath)
                
                localFeatureTable = localGDB.featureTables()[0] as! AGSGDBFeatureTable
                
                print ("how many features \(localFeatureTable.featureCount())")
                print("service layer \(localFeatureTable.serviceLayerName)")
                
                localFeatureTable = localGDB.featureTables()[0] as! AGSGDBFeatureTable
                
                print ("how many features \(localFeatureTable.featureCount())")
                print("service layer \(localFeatureTable.serviceLayerName)")
                
                print("field size \(localFeatureTable.fields().count)")
                for field in localFeatureTable.fields() {
                    print("field is \(field.name)")
                }
                
                let query: AGSQuery = AGSQuery()
                //query.geometry = self.mapView.maxEnvelope
                query.whereClause = "NAME Like 'Market%'"
                localFeatureTable.queryResultsWithParameters(query , completion: { (result, error ) -> Void in
                    
                    print("error is \(error); result count is \(result.count)")
                    let newResult = result as! [AGSGDBFeature]
                    for feat in newResult {
                        print("\(feat)")
                        //  feat.allAttributes()
                    }
                })
            } catch let error1 as NSError {
                error = error1
                print("what is error \(error)")
                self.routeTask = nil
            }
        }

View solution in original post

4 Replies
SuganyaBaskaran1
Esri Contributor

Can you try using AGSGDBFeature class instead of AGSFeature protocol? However, either one should work.

If that doesn't work, can you provide the stack trace of the crash? You can get this info by typing bt on the console window after the crash.

0 Kudos
MeiWei
by
New Contributor

AGSGDBFeature doesn't work. Here is the output from bt.

(lldb) bt

* thread #1: tid = 0xe656b7, 0x0000000111ab22a0 libunwind_sim.dylib`_Unwind_Resume + 27, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)

    frame #0: 0x0000000111ab22a0 libunwind_sim.dylib`_Unwind_Resume + 27

    frame #1: 0x000000010c93d94e OfflineRouting`-[AGSGDBFeature attributeForStdStringKey:] + 1310

    frame #2: 0x000000010c93cf0b OfflineRouting`-[AGSGDBFeature allAttributes] + 267

    frame #3: 0x000000010c93bbc6 OfflineRouting`-[AGSGDBFeature description] + 38

    frame #4: 0x000000011161fd5b libswiftFoundation.dylib`protocol witness for Swift.CustomStringConvertible.description.getter : Swift.String in conformance ObjectiveC.NSObject : Swift.CustomStringConvertible in Foundation + 43

    frame #5: 0x00000001111c8d3f libswiftCore.dylib`Swift._print_unlocked <A, B where B: Swift.OutputStreamType> (A, inout B) -> () + 559

    frame #6: 0x00000001111ac8c0 libswiftCore.dylib`Swift.String.init <A> (Swift.String.Type)(stringInterpolationSegment : A) -> Swift.String + 64

  * frame #7: 0x000000010c75d3d2 OfflineRouting`OfflineRouting.ViewController.(result=54113 values, error=nil) -> () -> ()).(closure #1) + 1682 at ViewController.swift:77

    frame #8: 0x000000010c75d533 OfflineRouting`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>, @owned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSError>) -> (@unowned ()) to @callee_owned (@in (Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>, Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSError>)) -> (@out ()) + 35 at ViewController.swift:0

    frame #9: 0x000000010c753a61 OfflineRouting`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>, @owned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSError>) -> (@unowned ()) to @callee_owned (@in (Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>, Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSError>)) -> (@out ()) + 81 at ViewController.swift:0

    frame #10: 0x000000010c75d567 OfflineRouting`reabstraction thunk helper from @callee_owned (@in (Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>, Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSError>)) -> (@out ()) to @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>, @owned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSError>) -> (@unowned ()) + 39 at ViewController.swift:0

    frame #11: 0x000000010c75d5f9 OfflineRouting`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>, @owned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSError>) -> (@unowned ()) to @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSArray>, @unowned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSError>) -> (@unowned ()) + 137 at ViewController.swift:0

    frame #12: 0x00000001117c0ba6 libdispatch.dylib`_dispatch_call_block_and_release + 12

    frame #13: 0x00000001117de7f4 libdispatch.dylib`_dispatch_client_callout + 8

    frame #14: 0x00000001117c78fb libdispatch.dylib`_dispatch_main_queue_callback_4CF + 949

    frame #15: 0x000000010e5aefe9 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9

    frame #16: 0x000000010e571eeb CoreFoundation`__CFRunLoopRun + 2043

    frame #17: 0x000000010e571486 CoreFoundation`CFRunLoopRunSpecific + 470

    frame #18: 0x00000001138619f0 GraphicsServices`GSEventRunModal + 161

    frame #19: 0x000000010fbbc420 UIKit`UIApplicationMain + 1282

    frame #20: 0x000000010c76063d OfflineRouting`main + 109 at AppDelegate.swift:18

    frame #21: 0x0000000111812145 libdyld.dylib`start + 1

0 Kudos
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

Hi Mei Wei,

I think the way you using the new Swift2 do...try...catch syntax is not correct and also I am not really sure the way that you load the bundle may has some issue.

I do based on an offlineRouting sample and add the method queryResultsWithParameters. It works fine and return 310 results. I would suggest to instantiate the AGSGDBGeodatabase & AGSGDBFeatureTable after the viewController

    var localGDB: AGSGDBGeodatabase!
    var localFeatureTable: AGSGDBFeatureTable!

Then use this format to handle the error based on The Swift Programming Language (Swift 2.1): Error Handling

do {

try expression

statements

} catch pattern 1 {

statements

} catch pattern 2 where condition {

statements

}

And I tested with this code and it works. Hope this can help.

        if let gdbPath = NSBundle.mainBundle().pathForResource("RuntimeSanFrancisco", ofType: "geodatabase"){
        
            do{
               try self.localGDB = AGSGDBGeodatabase(path: gdbPath)
                
                localFeatureTable = localGDB.featureTables()[0] as! AGSGDBFeatureTable
                
                print ("how many features \(localFeatureTable.featureCount())")
                print("service layer \(localFeatureTable.serviceLayerName)")
                
                localFeatureTable = localGDB.featureTables()[0] as! AGSGDBFeatureTable
                
                print ("how many features \(localFeatureTable.featureCount())")
                print("service layer \(localFeatureTable.serviceLayerName)")
                
                print("field size \(localFeatureTable.fields().count)")
                for field in localFeatureTable.fields() {
                    print("field is \(field.name)")
                }
                
                let query: AGSQuery = AGSQuery()
                //query.geometry = self.mapView.maxEnvelope
                query.whereClause = "NAME Like 'Market%'"
                localFeatureTable.queryResultsWithParameters(query , completion: { (result, error ) -> Void in
                    
                    print("error is \(error); result count is \(result.count)")
                    let newResult = result as! [AGSGDBFeature]
                    for feat in newResult {
                        print("\(feat)")
                        //  feat.allAttributes()
                    }
                })
            } catch let error1 as NSError {
                error = error1
                print("what is error \(error)")
                self.routeTask = nil
            }
        }
MeiWei
by
New Contributor

Thank you very much! This solves the error.

0 Kudos