Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realm where query doesn't give updated results in android. If application crashes it gives correct results. #1769

Closed
agarwalamit662 opened this issue Nov 13, 2015 · 6 comments
Labels

Comments

@agarwalamit662
Copy link

i have written a file which contains realm objects and entries. But realm query doesn't read from that file in android. But it is able to read if application crashes. I want to ask you how to get newer entries in realm file. My realm file returns null results without app crash and suddenly gives results after app crashes.

@kneth
Copy link
Contributor

kneth commented Nov 13, 2015

Can you share your code so we can see how you get a Realm instance, how you add objects to the Realm, and how you query it?

@agarwalamit662
Copy link
Author

hey

There are two files...

ListenerService.java listens from the mobile code and put the byte array
which contains Realm Objects and entries, into the file "DWearRealmFile".

In RealmDbService.java i use realm object instance. and query the contents
from the file in the same location.
The query returns 0 results.

package com.example.rdhule.myapplication.services;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.example.rdhule.myapplication.DailyLog;
import com.example.rdhule.myapplication.LauncherActivity;
import com.example.rdhule.myapplication.R;
import com.example.rdhule.myapplication.realm.ParentRealmObject;
import com.example.rdhule.myapplication.realm.RepeatingRealmAlert;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;

/**

  • Created by mkongari on 9/18/2015.
    */
    public class ListenerService extends WearableListenerService {
    //Remember to decler it as well in Manifest.xml
    private static final String TAG = ListenerService.class.getSimpleName();

    @OverRide
    public void onCreate() {
    super.onCreate();

    }

    @OverRide
    public void onDataChanged(DataEventBuffer dataEvents) {

    String WEARABLE_DATA_PATH = "/" + getString(R.string.RealmFileName);
    
    DataMap dataMap;
    for (DataEvent event : dataEvents) {
    
        // Check the data type
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            // Check the data path
            Log.e("Data is changed ","please change");
            Log.e("Data is changed ","please change");
            Log.e("Data is changed ","please change");
            String path = event.getDataItem().getUri().getPath();
            if (path.equals(WEARABLE_DATA_PATH)) {}
            dataMap =
    

    DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
    byte[] realmFileAsset = dataMap.getByteArray("RealmDatabase");

            if(realmFileAsset != null){
                toFile(realmFileAsset);
                Log.i(TAG, "DataMap received on watch: " + dataMap);
                Log.e(TAG, "DataMap received on watch: " + dataMap);
                Log.e(TAG,"DataMap received on watch: " + dataMap);
            }
        }
    }
    

    }

    private void toFile(byte [] byteArray){
    File writableFolder = ListenerService.this.getFilesDir();
    File realmFile = new File(writableFolder, "DWearRealmFile");
    if (realmFile.exists()) {

        Realm realm=
    

    Realm.getInstance(getApplicationContext(),"DWearRealmFile");
    realm.beginTransaction();

        //realm.where(ParentRealmObject.class).f
       // RealmResults<ParentRealmObject> parentRealmObjects =
    

    realm.where(ParentRealmObject.class).findAll();
    // parentRealmObjects.removeAll(parentRealmObjects);

        realm.clear(ParentRealmObject.class);
    
        realm.commitTransaction();
    
        realmFile.delete();
        //change on 12th november realm.delfile commented and
    

    realmFile.delete uncommented.
    // Realm.deleteRealmFile(getApplicationContext(),"DWearRealmFile");
    Log.e("in if", "in if");
    Log.e("in if","in if");
    realmFile = new File(writableFolder,"DWearRealmFile");

    }
    try {
        FileOutputStream fos = new FileOutputStream(realmFile.getPath());
        Log.e("filepath",realmFile.getPath());
        Log.e("filepath", realmFile.getPath());
        fos.write(byteArray);
    
        fos.close();
    
        Log.e("file written ", "File written");
        Log.e("file written ","File written");
        Log.e("file written ","File written");
        String fContents = readFile(realmFile.getPath());
        Log.e("file contents",fContents);
        Log.e("file contents",fContents);
    
    
    
    }
    catch (java.io.IOException e) {
        Log.d(TAG, "toFile exception: " + e.getLocalizedMessage());
    }
    

    }

    @OverRide
    public void onMessageReceived(MessageEvent messageEvent) {
    super.onMessageReceived(messageEvent);

    if (messageEvent.getPath().equals("/command_msg")) {
    
    
    }
    

    }

    String readFile(String fileName) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = br.readLine();
        }
        return sb.toString();
    } finally {
        br.close();
    }
    

    }

}

RealmDBService.java

package com.example.rdhule.myapplication.services;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.example.rdhule.myapplication.DailyLog;
import com.example.rdhule.myapplication.realm.MedicationRealm;
import com.example.rdhule.myapplication.realm.ParentRealmObject;
import com.example.rdhule.myapplication.realm.RepeatingRealmAlert;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmList;
import io.realm.RealmResults;

/**

  • Created by tbalakdaswagh on 9/21/2015.
    */
    public class RealmDBService {
    public List ans;
    public List findAllMedication(Context context){
    // ParseRealmService.syncParseToRealmDB(context, "DWearRealmFile");
    Realm realm = Realm.getInstance(context, "DWearRealmFile");
    realm.refresh();
    RealmResults parentRealmObjects=
    realm.where(ParentRealmObject.class).findAll();
    // RealmResults medicationRealmList =
    realm.where(MedicationRealm.class).findAll();
    Toast.makeText(context, "Data Found Parent : " +
    parentRealmObjects.size(), Toast.LENGTH_LONG).show();
    Toast.makeText(context, "Data Found Medication: " +
    parentRealmObjects.get(0).getMedicationRealmList().size(),
    Toast.LENGTH_LONG).show();
    realm.setAutoRefresh(true);
    realm.close();
    return parentRealmObjects.get(0).getMedicationRealmList();
    }

    public List
    findAllRepeatingRealmAlert(Context context,File writableFolder){
    // ParseRealmService.syncParseToRealmDB(context, "DWearRealmFile");
    Log.e("Here in findAllRe", "findAllRepeat");
    Log.e("Here in findAllRe", "findAllRepeat");

    Realm realm;
    try {
        //final RealmList<RepeatingRealmAlert> ans;
    
        realm = Realm.getInstance(writableFolder,"DWearRealmFile");
                //Realm.getInstance(context, "DWearRealmFile");
        realm.refresh();
    
        realm.setAutoRefresh(true);
        //realm.beginTransaction();
        RealmResults<ParentRealmObject> parentRealmObjects =
    

    realm.where(ParentRealmObject.class).findAll();

       // realm.where(ParentRealmObject.class).
        Log.e("parent obj ",parentRealmObjects.toString());
        Log.e("parent obj ",parentRealmObjects.toString());
        //parentRealmObjects.iterator();
        if(parentRealmObjects != null) {
            Log.e("Size parentRealmObj ",
    

    String.valueOf(parentRealmObjects.size()));
    Log.e("Size parentRealmObj ",
    String.valueOf(parentRealmObjects.size()));
    Log.e("Size parentRealmObj ",
    String.valueOf(parentRealmObjects.size()));
    if (parentRealmObjects.size() > 0)
    for (int i = parentRealmObjects.size() - 1; i >= 0; i--) {
    if (i == 0 && (parentRealmObjects.get(i) != null)) {
    // ans =
    parentRealmObjects.get(i).getRepeatingRealmAlertList();
    // realm.close();
    // realm.commitTransaction();
    return
    parentRealmObjects.get(i).getRepeatingRealmAlertList();
    }
    }
    }

        return null;
    }
    catch (Exception e)
    {
        Toast.makeText(context, "Exception is: " + e.toString(),
    

    Toast.LENGTH_LONG).show();
    Log.e("exception is ",e.toString());
    Log.e("exception is ",e.toString());
    return null;
    }

    // return null;
    }

}

On Fri, Nov 13, 2015 at 1:35 PM, Kenneth Geisshirt <[email protected]

wrote:

Can you share your code so we can see how you get a Realm instance, how
you add objects to the Realm, and how you query it?


Reply to this email directly or view it on GitHub
#1769 (comment).

@kneth
Copy link
Contributor

kneth commented Nov 16, 2015

It is not possible to access a Realm file from two separate processes - see issue #1091.

@agarwalamit662
Copy link
Author

But its a where query only... I am not writing any results on wear...??
On Nov 16, 2015 14:00, "Kenneth Geisshirt" [email protected] wrote:

It is not possible to access a Realm file from two separate processes -
see issue #1091 #1091.


Reply to this email directly or view it on GitHub
#1769 (comment).

@kneth
Copy link
Contributor

kneth commented Nov 16, 2015

A service can run as a separate process. How is it set up in your manifest?

@kneth
Copy link
Contributor

kneth commented Nov 30, 2015

Closing due to inactivity. Don't hesitate to reopen or create a new issue if you still experience the issue.

@kneth kneth closed this as completed Nov 30, 2015
@kneth kneth removed the Pending label Nov 30, 2015
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants