Realm android executeTransactionAsync不调用onSuccess

时间:2016-06-27 07:24:21

标签: android realm

我正在尝试添加50条生成随机数据的学生(领域对象)记录。永远不会调用onSuccess。我需要在成功插入时执行一些任务....

   public class MainActivity extends AppCompatActivity {

private static final String ALPHA_NUMERIC_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

RealmList<Student> mStudentList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ((Button) findViewById(R.id.simple_insert)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,SimpleInsertActivity.class));
        }
    });

    ((Button) findViewById(R.id.buld_insert)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mStudentList = new RealmList<>();
            final Realm mRealm = Realm.getDefaultInstance();


            mRealm.executeTransactionAsync(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    for(int i=0;i<50;i++){
                        String s =randomAlphaNumeric(8);
                        Log.i(getClass().getSimpleName(),"------------------------name : " + s);

                        Student student = realm.createObject(Student.class);//new Student();
                        student.setName(randomAlphaNumeric(8));
                        mStudentList.add(student);
                    }
                    Log.i(getClass().getSimpleName(),"----------------------------inserting data");
                    realm.copyToRealmOrUpdate(mStudentList);
                }
            }, new Realm.Transaction.OnSuccess() {
                @Override
                public void onSuccess() {

                    Toast.makeText(MainActivity.this, "inserted successfully", Toast.LENGTH_SHORT).show();
                    Log.i(getClass().getSimpleName(),"----------------------------after success :: " +
                    mRealm.where(Student.class).findAll().size());
                }
            }, new Realm.Transaction.OnError() {
                @Override
                public void onError(Throwable error) {
                    Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
                    Log.e(MainActivity.this.getClass().getSimpleName(),"-----------------------error : " + error.toString());
                }
            });
            mRealm.close();


        }
    });

    ((Button) findViewById(R.id.delete)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AppClass.deleteRealmDatabase();
        }
    });
}

public static String randomAlphaNumeric(int count) {
    StringBuilder builder = new StringBuilder();
    while (count-- != 0) {
        int character = (int)(Math.random()*ALPHA_NUMERIC_STRING.length());
        builder.append(ALPHA_NUMERIC_STRING.charAt(character));
    }
    return builder.toString();
}

}

public class Student extends RealmObject {
public static final String KEY_ROLL_NO = "rollNo";
public static final String KEY_NAME = "name";
@PrimaryKey
String rollNo;
String name;

public Student(String s) {
    this.rollNo = UUID.randomUUID().toString();
    this.name = s;
}

public Student() {

}

public String getRollNo() {
    return rollNo;
}

public void setRollNo(String rollNo) {
    this.rollNo = rollNo;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

日志: ...............  [06-27 05:13:55.327 28186:28560 I /] ------------------------名称:NRW29XS7

[06-27 05:13:55.327 28186:28560 I /] ------------------------姓名:2MS9QSB0

[06-27 05:13:55.327 28186:28560 I /] ------------------------姓名:BJ2JHHHV

[06-27 05:13:55.327 28186:28560 I /] ----------------------------插入数据

1 个答案:

答案 0 :(得分:2)

我一直在关闭主线程中的Realm实例,所以它没有获取实例而且onSuccess没有被调用