How to make Loader update with sqlite view?

429 views Asked by At

I got a problem with LoaderManager. I have some tables in sqlite database, and also have representation view, which get data from tables.

I also had implemented LoaderManager, exactly as in that excellent guide and it works perfectly for table.

But I want to update one table, and get updated result not from it, but from sqlite view, related to updated table. In this case LoaderManager, seems, not working (onLoadFinished callback does not fired)

My updated table schema:

 CREATE TABLE [table_scan] (
   [_id] INTEGER PRIMARY KEY AUTOINCREMENT,
   [NR_ID] INTEGER NOT NULL,
   [T_ID] INTEGER NOT NULL,
   [Color_ID] INTEGER NOT NULL,
   [R_ID] INTEGER NOT NULL,
   [Barcode] TEXT NOT NULL,
   [NumberSeat] INTEGER,
   [Date] DATETIME NOT NULL DEFAULT(DATETIME('now', 'localtime')),
   [Deleted] INTEGER NOT NULL DEFAULT '0',
   [Status] INTEGER NOT NULL DEFAULT '0',
   [Export] INTEGER NOT NULL DEFAULT '0');

My sqlite view:

CREATE VIEW [view_scan] AS SELECT _id, Barcode, Status, Deleted, NumberSeat,
 goods_catalog.T_Articul, colors_catalog.Color_Name, sizes_catalog.R_Name
 FROM table_scan
   INNER JOIN goods_catalog ON goods_catalog.T_ID = table_scan.T_ID
   INNER JOIN colors_catalog ON colors_catalog.Color_ID = table_scan.Color_ID
   INNER JOIN sizes_catalog ON sizes_catalog.R_ID = table_scan.R_ID
 WHERE Deleted = 0;
2

There are 2 answers

0
Sash0k On BEST ANSWER

Found a workaround. Not very clean, but works:

Uris for LoaderManager init and for update data must be equals. So, we need to update view. We can create trigger for that.

First, modify view to get all needed columns:

CREATE VIEW [view_scan] AS SELECT
  table_scan._id, table_scan.NR_ID, 
  table_scan.T_ID,table_scan.Color_ID,
  table_scan.R_ID, table_scan.Barcode,
  table_scan.NumberSeat, table_scan.Deleted, 
  table_scan.Status,
  goods_catalog.T_Articul,
  colors_catalog.Color_Name,
  sizes_catalog.R_Name
FROM table_scan
 INNER JOIN goods_catalog ON goods_catalog.T_ID = table_scan.T_ID
 INNER JOIN colors_catalog ON colors_catalog.Color_ID = table_scan.Color_ID
 INNER JOIN sizes_catalog ON sizes_catalog.R_ID = table_scan.R_ID
WHERE Deleted = 0;

Next, create trigger for view_scan:

CREATE TRIGGER insert_view_scan
  instead of insert on view_scan 
   begin
    insert into table_scan(NR_ID,T_ID,Color_ID,R_ID,Barcode,NumberSeat,Status)
    values(new.NR_ID, new.T_ID, new.Color_ID, new.R_ID, new.Barcode, new.NumberSeat, new.Status);
   end;

Now, we can update data to view_scan instead of table_scan and LoaderManager works fine.

0
sui4556 On

It's so easy to do like below, no need to use trigger!

public Cursor query(...) {
    ...
    case view_scan
    ...

    cursor.setNotificationUri(resolver, AUTHORITY_URI);
}
public Uri insert(Uri uri, ContentValues values) {
    ...
    case table_scan:
    ...
    cursor.setNotificationUri(resolver, AUTHORITY_URI);
}