It is interesting point that it sometimes performs insert operation correctly. I do not know why and how this situation can be happens. So, I could not figure out where I made a mistake(s). Here is my project files.
1) SentFilesDao.java
@Dao
public interface SentFilesDao {
@Query("SELECT id, name, type, status, dateTime FROM sent")
LiveData<List<SentFilesEntity>> getAllSentFiles();
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(SentFilesEntity file);
@Query("DELETE FROM sent")
void deleteAllRecords();
}
2) SentFilesRepository.java
public class SentFilesRepository {
private SentFilesDao mSentFilesDao;
private LiveData<List<SentFilesEntity>> mAllSentFiles;
SentFilesRepository(Application application) {
AppDatabase db = AppDatabase.getDatabase(application);
mSentFilesDao = db.mSentFilesDao();
mAllSentFiles = mSentFilesDao.getAllSentFiles();
}
LiveData<List<SentFilesEntity>> getAllSentFiles() { return mAllSentFiles; }
public void insert(SentFilesEntity sentFilesEntity) {
new SentFilesRepository.insertAsyncTask(mSentFilesDao).execute(sentFilesEntity);
}
private static class insertAsyncTask extends AsyncTask<SentFilesEntity, Void, Void> {
private SentFilesDao mAsyncTaskDao;
insertAsyncTask(SentFilesDao dao) {
mAsyncTaskDao = dao;
}
@Override
protected Void doInBackground(final SentFilesEntity... params) {
mAsyncTaskDao.insert(params[0]);
Log.e("doInBackground: ", "reached here!"); // no any log in Logcat about this. Weird...
return null;
}
}
}
3) SentFilesEntity.java
@Entity(tableName = "sent")
public class SentFilesEntity {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
public int s_f_id;
@ColumnInfo(name = "name")
public String s_f_name;
@ColumnInfo(name = "type")
public String s_f_type;
@ColumnInfo(name = "status")
public String s_f_operation_status;
@ColumnInfo(name = "dateTime")
public String s_f_time;
}
4) SentFilesViewModel.java
public class SentFilesViewModel extends AndroidViewModel {
private SentFilesRepository mRepository;
private LiveData<List<SentFilesEntity>> mAllSentFiles;
public SentFilesViewModel(Application application) {
super(application);
mRepository = new SentFilesRepository(application);
mAllSentFiles = mRepository.getAllSentFiles();
}
public LiveData<List<SentFilesEntity>> getAllSentFiles() { return mAllSentFiles; }
public void insert(SentFilesEntity sentFile) { mRepository.insert(sentFile); }
}
What I am trying to do:
My program sends selected files from one device to another one through WiFi Direct. As file received or sent, I want to save some information about file (such as file name, file type (image, video, music, APK and etc.), send/receive date and time) and operation (succeeded or failed). I can handle all these things.
What is the problem?
Well, getting all the properties (those I mentioned above), is not end. Main problem is saving that data to database. To do that I created some required files (I shared their codes above). When file sent, I am trying to insert related information to database. But it does not insert it. That is the my problem.
Well, main codes are too long to post here. However I posted my latest codes to my GitHub project and the main logic starts from here and it connected to FileStatisticsActivity
.
p.s: Maybe sharing the project link is not ethic, I am so sorry for that.
Thanks.