tmw
tmw

Reputation: 1484

Can not delete realm object on NavigatorIOS right click action

I am trying to add a single sign on feature in a react-native app. I am using realm for data persistence. It is working fine with sign in process and stores data when user is login in fist time. But I want to remove user object from local storage when I click on logout button in navigator top right corner.

home screen

I am getting an Invalid arguments exception when I try to delete user object from realm storage in onRightButtonPress event. Here is code for this event in NavigatorIOS.

<NavigatorIOS
    barTintColor="#000000"
    tintColor="#fff"
    titleTextColor="#fff"
    ref={(ref) => this._navigator = ref}
    style={{flex: 1,backgroundColor: '#fff'}}
    initialRoute={{
        title: 'NOOZOO',
        component: Home,
        leftButtonIcon: require('../images/menu.png'),
        onLeftButtonPress: () => { this._drawer.open() },
        rightButtonTitle: 'Logout',
        onRightButtonPress: () => {alert('Logout');
                                   console.log({name: global.__user.name,token: global.__user.token});
                                   var users = realm.objects('User');
                                   console.log(users.length);
                                   console.log(users[0].isValid());
                                   realm.write(()=>{
                                    realm.delete('User', users[0]);
                                  });
                                   console.log('deleted');
                                  this._navigate({'title':'Company','routeName':'Home','active':false,'iconUrl': require('../images/check.png'),'newView':false}); 

                                  console.log(users);
                                  },
    }}/>

I am printing some values to consoles in order to verify if realm is accessible and have some object in storage, which seems fine as in following image.

Console output when Logout is pressed

I don't know if I am doing something wrong with realm method call or something. I have seen the docs for realm and also read issue about it on github but was unable to resolve this issue. Any help would be appreciated.

Upvotes: 0

Views: 230

Answers (1)

Ari
Ari

Reputation: 1447

The call do delete does not take the object type as its first argument. Try changing that line to this:

realm.delete(users[0]);

Upvotes: 1

Related Questions