3

In ActiveRecord, there's a concept of locking records for updates to ensure that a stale object doesn't get saved to the database.

Is there equivalent functionality in ActiveAndroid? If so, is there a link showing how to do it? If not, what would be the best approach to prevent stale objects from getting saved to the db?

WindsurferOak
  • 4,861
  • 1
  • 31
  • 36
  • Do you mean not saving duplicates? – user2511882 Feb 09 '14 at 03:09
  • Not exactly. Let's say you query an object, A = Foo(1), and then you query B = Foo(1). You now have two objects (A & B) representing the record with id=1. Now, if you modify A then save, B will now become stale because it doesn't have the latest changes that were made to A. – WindsurferOak Feb 09 '14 at 19:40

2 Answers2

0

ActiveAndroid has a concept of beginTransaction() and endTransaction() which basically are similar to the locking record concept in activeRecord. By using the beginTransaction() you ensure that while any changes being made to a method or data is being accessed, no other object can interfere with the process. Once the endTransaction() is executed, the method is "unlocked" and other objects can start to interact with it.

user2511882
  • 9,022
  • 10
  • 51
  • 59
  • transactions serve a different purpose. A transaction is usually used to batch updates/inserts/deletes so that either all succeed or all fail. It also improves performance of these operations. It will not prevent the update of a stale object. – WindsurferOak Feb 10 '14 at 18:56
  • Pardon me if i am wrong here since i am learning to use ActiveAndroid myself, but as far as my knowledge activeAndroid maps up to SQLite which on its own does the locking of the database while reading,writing etc... – user2511882 Feb 10 '14 at 19:11
  • yes, that's correct; but again, as far as I know, you can still update with stale data. – WindsurferOak Feb 10 '14 at 19:25
  • On ActiveAndroid? No. I have tried it. And it wont let you access a data while its inProgress. So the other object has to wait – user2511882 Feb 10 '14 at 19:37
  • I understand what you mean. Let me explain more. Assume you have A = Foo(1) and B = Foo(1). Modify A and then save A in a transaction. Now, after the transaction is over, evaluate B. Does B have the updates that were made to A? What happens when you save B? Will saving B override the changes that A made? The point here isn't concurrent access to the DB. I understand what db locking does, the point here is that B has an old copy of the record. In ActiveRecord, depending on the chosen strategy, you will get an error when you save B because B holds stale data. – WindsurferOak Feb 10 '14 at 19:53
  • Maybe this comment is late, but if you do find something, please update the answer. I couldn't get any specific detail as of today – user2511882 Mar 14 '14 at 23:48
0

I don't know the answer, but what I am doing is, declaring an unique field in my model class, and onUniqueConflict replacing the old record with the new one.

    @Column(name = "userId", notNull = true,unique = true,onUniqueConflict = Column.ConflictAction.REPLACE)
    private String userId;
Ratul Ghosh
  • 1,512
  • 9
  • 15