0

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.

tmw
  • 1,424
  • 1
  • 15
  • 26

1 Answers1

1

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

realm.delete(users[0]);
Ari
  • 1,439
  • 10
  • 7
  • this solution worked but I am wondering how realm automatically infer based on object without telling it about collection Class. – tmw Jun 14 '16 at 08:16
  • Since the object is created from Realm we can tell what type of object it is and delete it appropriately. – Ari Jun 15 '16 at 00:52
  • from your answer, we are not explicitly mentioning the object type is 'User'. – tmw Jun 15 '16 at 07:44
  • Exactly. That information is stored internally in each User object returned when accessing elements in the `Results` object `users` – Ari Jun 16 '16 at 20:29