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

Added lifecycle events: preInsert(), preUpdate(), preSave(), and their postXY counterparts #333

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/com/activeandroid/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,37 @@ public final void delete() {
Cache.getContext().getContentResolver()
.notifyChange(ContentProvider.createUri(mTableInfo.getType(), mId), null);
}

public final Long save() {
boolean isNew = (mId == null);

if (isNew) {
preInsert();
} else {
preUpdate();
}

preSave();
Long result = doSave();
postSave();

if (isNew) {
postInsert();
} else {
postUpdate();
}

return result;
}

protected void preInsert() {}
protected void preUpdate() {}
protected void preSave() {}
protected void postSave() {}
protected void postInsert() {}
protected void postUpdate() {}

private final Long doSave() {
final SQLiteDatabase db = Cache.openDatabase();
final ContentValues values = new ContentValues();

Expand Down
3 changes: 3 additions & 0 deletions src/com/activeandroid/ModelInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ else if (ReflectionUtils.isTypeSerializer(discoveredClass)) {
catch (IllegalAccessException e) {
Log.e("IllegalAccessException", e);
}
catch (IncompatibleClassChangeError e) {
Log.e("IncompatibleClassChangeError", e);
}
}
}
}
2 changes: 1 addition & 1 deletion src/com/activeandroid/annotation/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {

public static final String DEFAULT_ID_NAME = "Id";
public static final String DEFAULT_ID_NAME = "id";
public String name();
public String id() default DEFAULT_ID_NAME;
}