0

Pardon me for asking such a silly question. I am trying to use Realm DB for my app and I am stuck at how to see my database on realm browser. Also I tried to see the path and find it in my files. I didn't find any Realm file. How do I go about it? Also I am interested to see my result set and the queries, which presently I am having trouble with. Here is the code. I am open to suggestions and better implementation practices. Also is it better to make a separate schema file and a separate file where I initialise my schema? Cannot see my filter results.

const testScema = {
  name: 'profile',
  properties: {
    name: 'string',
    age: 'int',
    sport: 'string',
  }
};

let realmObj = new realm({schema: [testScema]});

export default class test extends Component {
  constructor(props){
    super(props);
    this.state = {
      name: '',
      sport: '',
      age: '',
    }
  }

  insert(){
    var age = parseInt(this.state.age);
    realmObj.write(() => {
      let myProf = realmObj.create('profile',{
        name: this.state.name,
        age: age,
        sport: this.state.sport,
      });
    });
    let allProfiles = realmObj.objects('profile');
    let filterRes = allProfiles.filtered('name BEGINSWITH "h"');
    this.refs.name.setNativeProps({text: ''});
    this.refs.age.setNativeProps({text: ''});
    this.refs.sport.setNativeProps({text: ''});
    console.log("filter result", filterRes);
    console.log("items in db", realmObj.objects('profile').length);
  }

  delete(){
    let allProfiles = realmObj.objects('profile');
    realmObj.write(() => {
    realmObj.delete(allProfiles);
    });
  }

  render() {
   return (
     <View>
      <Text>For Realm testing</Text>
      <View style={{flexDirection: 'row'}}>
        <Text style={{flex: 1}}>Enter Name: </Text>
        <TextInput ref="name" onChangeText={(name) => this.setState({name})} style={{flex: 1}}/>
      </View>
      <View style={{flexDirection: 'row'}}>
        <Text style={{flex: 1}}>Enter Age: </Text>
        <TextInput ref="age" keyboardType="numeric" onChangeText={(age) => this.setState({age})} style={{flex: 1}}/>
      </View>
      <View style={{flexDirection: 'row'}}>
        <Text style={{flex: 1}}>Enter Favourite Sport: </Text>
        <TextInput ref="sport" onChangeText={(sport) => this.setState({sport})} style={{flex: 1}}/>
      </View>
      <View style={{flexDirection: 'row', justifyContent: 'space-around'}}>
        <Button style={{flex: 1}} title="INSERT" onPress={() => this.insert()}/>
        <Button style={{flex: 1}} title="DELETE" onPress={() => this.delete()}/>
      </View>
      <ListView
        dataSource={ds.cloneWithRows(realmObj.objects('profile'))}
        renderRow={(data) => <Text>{data.defaultPath}</Text>}/>
     </View>
   );
  }
}
Oriol Roma
  • 329
  • 1
  • 5
  • 9
Y_J
  • 81
  • 1
  • 8

1 Answers1

1

If you are running on Android, you have to copy realm file from your application's internal storage and paste it somewhere on your computer and then browse file using realm browser.

You can find realm file at data/data/$application_package_name/files path.

Update: This answers your question. https://stackoverflow.com/a/28465803/1282812

Community
  • 1
  • 1
Kalpesh Patel
  • 1,638
  • 1
  • 20
  • 35
  • Thank you for the answer... but where do i find the file on my phone's internal storage.. because in data/data/ only cache is present which is empty... is that the file ? the cache directory – Y_J Mar 24 '17 at 12:25
  • You will need rooted phone. If you don't have rooted phone, you can install your app in emulator and then access emulator file system using Android device monitor. – Kalpesh Patel Mar 24 '17 at 12:29
  • so apart from my phone being rooted i cannot see the .realm file ? seems illogical – Y_J Mar 24 '17 at 12:38
  • You can see it on emulator. – Kalpesh Patel Mar 24 '17 at 12:41
  • Well thanks you took soo much effort to make it clear to me but.. it still doesnt solve my problem because .. i am using React Native (javascript) .. not native android. so i dont think it will work.. but still i am gonna try. – Y_J Mar 24 '17 at 13:29
  • Yes, I understand, But I thing realm react native library would use native APIs under the hood. – Kalpesh Patel Mar 24 '17 at 13:35
  • Did you try on emulator or phone? – Kalpesh Patel Mar 24 '17 at 13:36
  • yes, we can assume they may be using native APIs but still after pulling i didnt get anything. i tried it on my phone(unrooted). – Y_J Mar 24 '17 at 13:45
  • You will not able to get content of internal storage by using that command on non-rooted phone. You would need rooted phone or emulator. – Kalpesh Patel Mar 24 '17 at 13:55
  • so while using device (non-rooted), i cannot view my realm file ? what if i give a custom path.. but its giving an error , unable to to open realm at path "random path": mkdir_dir() failed... – Y_J Mar 25 '17 at 07:56
  • You can see realm file from phone also, but you will have to write file copy code to copy realm file and paste in external drive. I am not sure whether you can do the same on iOS or not. Also you can not pass `random path` as db path. it should be proper file path. – Kalpesh Patel Mar 25 '17 at 18:33
  • listen.. i used the emulator after wasting a lot of time on how to see it on phone.. and yes i can see the realm file and use it in my realm browser, but it does not change dynamically. i will look more into it and how to use it dynamcially. like if i insert a record it changes in the realm browser too. All suggestions are welcome. @Kaplesh Patel – Y_J Mar 27 '17 at 07:04