chat-sdk / chat-sdk-ios

Chat SDK iOS - Open Source Mobile Messenger
http://sdk.chat
Other
913 stars 276 forks source link

crash on creating the thread for the new chat #438

Open vijay21021993 opened 4 years ago

vijay21021993 commented 4 years ago

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-03-29 14:43:27.468067+0530 Wink[5975:33198] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23baa1ee __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50864b20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23c3cb71 _CFThrowFormattedException + 194
    3   CoreFoundation                      0x00007fff23c3b683 -[__NSArrayM insertObject:atIndex:].cold.1 + 35
    4   CoreFoundation                      0x00007fff23abd1ff -[__NSArrayM insertObject:atIndex:] + 1263
    5   ChatSDK                             0x0000000106865e33 -[BAbstractCoreHandler fetchThreadWithUsers:] + 211
    6   ChatSDK                             0x00000001068662be -[BAbstractCoreHandler fetchOrCreateThreadWithUsers:name:] + 94
    7   Wink                                0x00000001054e6f66 $s4Wink8myChatVCC9tableView_12cellForRowAtSo07UITableF4CellCSo0kF0C_10Foundation9IndexPathVtF + 31494
    8   Wink                                0x00000001054e98e5 $s4Wink8myChatVCC9tableView_12cellForRowAtSo07UITableF4CellCSo0kF0C_10Foundation9IndexPathVtFTo + 165
    9   UIKitCore                           0x00007fff47778d93 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 781
    10  UIKitCore                           0x00007fff4774210c -[UITableView _updateVisibleCellsNow:] + 3081
    11  UIKitCore                           0x00007fff477621df -[UITableView layoutSubviews] + 194
    12  UIKitCore                           0x00007fff47a52ad5 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    13  QuartzCore                          0x00007fff2b06e91d -[CALayer layoutSublayers] + 255
    14  QuartzCore                          0x00007fff2b073323 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
    15  QuartzCore                          0x00007fff2b07fa7c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
    16  QuartzCore                          0x00007fff2afc6e54 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
    17  QuartzCore                          0x00007fff2affc32f _ZN2CA11Transaction6commitEv + 643
    18  QuartzCore                          0x00007fff2affcc96 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
    19  CoreFoundation                      0x00007fff23b0c667 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    20  CoreFoundation                      0x00007fff23b070fe __CFRunLoopDoObservers + 430
    21  CoreFoundation                      0x00007fff23b0777a __CFRunLoopRun + 1514
    22  CoreFoundation                      0x00007fff23b06e66 CFRunLoopRunSpecific + 438
    23  GraphicsServices                    0x00007fff38346bb0 GSEventRunModal + 65
    24  UIKitCore                           0x00007fff47578dd0 UIApplicationMain + 1621
    25  Wink                                0x0000000105667cfb main + 75
    26  libdyld.dylib                       0x00007fff516ecd29 start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
armaghan84 commented 4 years ago

I am having the same issue.

armaghan84 commented 4 years ago

Hi, I found a way to create thread

_ = BChatSDK.auth()?.authenticate().thenOnMain({ success -> Any? in
         /// Create Thread Here
    }, nil)

@vijay21021993

vijay2102 commented 4 years ago

I am trying to create a thread with the two user to make one to one chat. The thread is creating on my controller where I link them and start a chat.

SamQuest commented 4 years ago

you need something like this

`

        let indexes: [Any] = [bUserEmailKey]

        let promise = BChatSDK.search()?.users(forIndexes: indexes, withValue: "myContact@example.com", limit: 1, userAdded: {
            result in
            if let user = result {
                let users: [Any] = [user]
                let thread = BChatSDK.core()?.fetchOrCreateThread(withUsers: users, name: user.name())
                if let vc = BChatSDK.ui()?.chatViewController(with: thread) { 
                    controller.navigationController?.pushViewController(vc, animated: true)
                } else {
                    let error = ... // define custom error
                    _ = self.handleError(error)
                }
            } else {
                    let error = ... // define custom error
                _ = self.handleError(error)
            }
        })
        _ = promise?.thenOnMain({ result in
            return result
        }, { error in
            return self.handleError(error)
        })

`

and for this to work your target user must be already in firebase database

vijay2102 commented 4 years ago

The above looks good but every user doesn't have email they may be signIn with their social account or mobile no.All the user have firebaseID can we use that instead of email?

SamQuest commented 4 years ago

The above looks good but every user doesn't have email they may be signIn with their social account or mobile no.All the user have firebaseID can we use that instead of email?

explore indexes and search api.

bensmiley commented 4 years ago

If you want to get a user for a given ID, you can use something like this:

-(id<PUser>) getUserForEntityID: (NSString *) entityID {
    id<PUser> user = [BChatSDK.db fetchOrCreateEntityWithID:entityID withType:bUserEntity];
    [BChatSDK.core observeUser:user];
    return user;
}

The first line gets the user object from the database or creates it if it doesn't exist. The second line adds the Firebase listeners to that user.

vijay2102 commented 4 years ago

The crash is getting on creating and fetching the thread throw the users. ( BChatSDK.core()?.createThread(withUsers: userArray as [Any],) Getting the user was not the problem the thread creation method is the problem.

bensmiley commented 4 years ago

It looks like one of the users you're trying to add is Nil. Are you sure that you have let authentication finish before trying to make the thread?

_ = BChatSDK.auth()?.authenticate().thenOnMain({ success -> Any? in
         /// Create Thread Here
    }, nil)

If you try to make a thread before this, the current user will be Nil.

SamQuest commented 4 years ago

sorry about the previous example (i'm new to this framework) fetchOrCreateThread does not seem to sync with firebase try this approach

        let indexes: [Any] = [bUserEmailKey]

        let promUsers = BChatSDK.search()?.users(forIndexes: indexes, withValue: userInfo.email(), limit: 1, userAdded: {
            result in
            if let user = result {
                let users: [Any] = [user]
                let promThread = BChatSDK.core()?.createThread(withUsers: users, threadCreated: { (error, thread) in
                    if let thread = thread {
                        if let vc = BChatSDK.ui()?.chatViewController(with: thread) {
                            controller.navigationController?.pushViewController(vc, animated: true)
                        } else {
                            let error = ... // define custom error
                           _ = self.handleError(error)
                        }
                    } else if let error = error {
                        _ = self.handleError(error)
                    }
                })
                _ = promThread?.thenOnMain({ result in
                    return result
                }, { error in
                    return self.handleError(error)
                })

            } else {
                let error = ... // define custom error
                _ = self.handleError(error)
            }
        })
        _ = promUsers?.thenOnMain({ result in
            return result
        }, { error in
            return self.handleError(error)
        })