Get All the Users

I’m trying to get a list of all the users to a macOS app using Swift 3. But for some reason it’s not working. I use the same code for one of my own classes.

users is an Array of BackendlessUser







p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}
span.s1 {font-variant-ligatures: no-common-ligatures}
span.s2 {font-variant-ligatures: no-common-ligatures; color: #ba2da2}
span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}
span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
span.s6 {font-variant-ligatures: no-common-ligatures; color: #3e1e81}
span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}
span.s8 {font-variant-ligatures: no-common-ligatures; color: #31595d}
span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}



 func getUsers() {

        if users.count > 0 {

            users.removeAll()

        }

        

        let dataStore = backendless!.data.of(BackendlessUser.ofClass())

        

        dataStore?.find(

            { (result: BackendlessCollection?) -> Void in

                let data = result?.getCurrentPage()

                

                for obj in data! {

                    let item = obj as! BackendlessUser

                    self.users.append(item)

                }

                

                debugPrint("Users are loaded: \(self.users.count) items")

                //                self.tableView.reloadData()

        },

            error:  {(fault: Fault?) -> Void in

                debugPrint("Server reported an error: \(String(describing: fault))")

        })

    }

Hello,

Could you please describe “it is not working” in greater details? Such as: are you getting an error? If not, are you getting any users back? How many are in the database? How many are returned?

Mark

Hi Mark.

It goes directly to ‘error’ but doesn’t show any error code.
There are only about 5 records and no one show.

Following https://backendless.com/feature-52-fetching-applications-user-objects/ doesn’t work either in Swift.

In the code in the screen shot, neither of the print statement get called.

Hi Jorgen,

please try this snippet:

backendless.persistenceService.of(BackendlessUser.ofClass()).find(
            { (users : BackendlessCollection!) -> () in
                print("Users have been fetched  (ASYNC): \(users)")
        },
            error: { (fault : Fault!) -> () in
                print("Server reported an error (ASYNC): \(fault)")
        })

Please let us know about the result.

Regards,
Stanislaw

Hi Stanislaw,

I , unfortunately, get the same problem.
Without calling the func, the app starts up fine and presents the main screen.
If I call it, either in viewDidLoad or viewDidAppear, the app crashes.

As you can see from the screenshot, the function is not hooked up to anything on the screen yet. I was just trying to get a result from it first.

Pod file:

Pods for BF4L Coach

pod ‘Backendless-osx-SDK’

Crash log;

2017-06-19 10:23:44.065107+0100 BF4L Coach[83468:4207357] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArray0 objectAtIndex:]: index 3 beyond bounds for empty NSArray’

*** First throw call stack:

(

0 CoreFoundation 0x00007fffa6bb72cb __exceptionPreprocess + 171

1 libobjc.A.dylib 0x00007fffbb9c248d objc_exception_throw + 48

2 CoreFoundation 0x00007fffa6bd223d -[__NSArray0 objectAtIndex:] + 93

3 BF4L Coach 0x00000001000853f0 -[BackendlessCache invokeAsync:method:args:responder:] + 124

4 BF4L Coach 0x00000001000123e2 TFC10BF4L_Coach6MainVC18fetchingUsersAsyncfT_T + 834

5 BF4L Coach 0x00000001000118af TFC10BF4L_Coach6MainVC13viewDidAppearfT_T + 31

6 BF4L Coach 0x00000001000118e2 TToFC10BF4L_Coach6MainVC13viewDidAppearfT_T + 34

7 AppKit 0x00007fffa4766264 -[NSViewController _sendViewDidAppear] + 41

8 AppKit 0x00007fffa4765f5b -[NSView(NSInternal) _windowDidOrderOnScreen] + 67

9 AppKit 0x00007fffa476600e -[NSView(NSInternal) _windowDidOrderOnScreen] + 246

10 AppKit 0x00007fffa4ecaa38 -[NSWindow _reallyDoOrderWindowAboveOrBelow:relativeTo:findKey:forCounter:force:isModal:] + 2532

11 AppKit 0x00007fffa47626ce -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1041

12 AppKit 0x00007fffa4762263 -[NSWindow orderWindow:relativeTo:] + 153

13 AppKit 0x00007fffa4814bab -[NSWindow makeKeyAndOrderFront:] + 111

14 QuickLookUI 0x00007fffac5e6519 -[QLSeamlessDocumentOpener showWindow:contentFrame:withBlock:] + 108

15 AppKit 0x00007fffa4813d37 -[NSWindowController showWindow:] + 689

16 AppKit 0x00007fffa45e6d2e NSApplicationMain + 1013

17 BF4L Coach 0x00000001000156cd main + 13

18 libdyld.dylib 0x00007fffbc2a7235 start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException

(lldb)

Screen Shot 2017-06-19 at 10.20.18.png

Hello Jorgen,

Could you please provide us your simplified Xcode test project so we could check this issue?

Regards, Olga

Hi Olga,

I moved the function to the Appdelegate and called it in applicationDidFinishLaunching, and there it works fine. Why it wouldn’t work in the view controller is mystery to me.
I have called other fetches from other viewcontrollers with no problem at all.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}
span.s1 {font-variant-ligatures: no-common-ligatures}

Can we mark this topic as resolved?

Yea, I can’t see that it any fault from Backendless side.

Just slightly stumped why it’s happening at all.