Refill TableView if db changes

I have a mysql db serverside and a php scripts that fetches data from the db. The app consumes the php and fills a tableview with rows from the db on createScene.

What is the best programmatic way to refill/rerender the rows in the tableview if the db changes? More specific, what functions do i need to have in the enterScene to make this happened?

Im having trouble with dividing the tableview code from corona api into create and enterscene, and hoping you guys can help me out :slight_smile:

This is the code for the tableView by the way. 

local function tableViewListener( event ) local phase = event.phase local row = event.target print( event.phase ) end -- Handle row rendering local function onRowRender( event ) local phase = event.phase local row = event.row local rowTitle = display.newText( row, " \> " .. data[row.index].Descr, 0, 0, nil, 14 ) rowTitle.x = row.x - ( row.contentWidth \* 0.5 ) + ( rowTitle.contentWidth \* 0.5 ) rowTitle.y = row.contentHeight \* 0.5 rowTitle:setTextColor( 0, 0, 0 ) end -- Handle touches on the row local function onRowTouch( event ) local phase = event.phase if "press" == phase then print( "Touched row:", event.target.index ) end end -- Create a tableView local tableView = widget.newTableView { top = 100, width = 320, height = 366, maskFile = "assets/mask-320x366.png", listener = tableViewListener, onRowRender = onRowRender, onRowTouch = onRowTouch, hideBackground = true, } group:insert( tableView ) --CREATE TABLEVIEW ROWS local function setupTableViewRows() for i = 1, numberOfRows do local isCategory = false local rowHeight = 40 local rowColor = { default = { 255, 255, 255 }, } local lineColor = { 150, 220, 220 } -- Make some rows categories if i == 25 or i == 50 or i == 75 then isCategory = true rowHeight = 24 rowColor = { default = { 150, 160, 180, 200 }, } end -- Insert the row into the tableView tableView:insertRow { isCategory = isCategory, rowHeight = rowHeight, rowColor = rowColor, lineColor = lineColor, } end native.setActivityIndicator( false ) end

If you want to re-render everything you can use the deleteAllRows function and then insert again as usual.  http://docs.coronalabs.com/daily/api/type/TableViewWidget/deleteAllRows.html

Im struggling with how the dataflows should look like. The app will probably work like this.

When the app launches, it connect to the php server function and request data. The data is stored in an app db.

When the user enters the tableview scene the tableview is built up from rows in the app db. 

If the user leave the scene, but it stays in the memory so when the user enters again the createscene will never fire. In this case if the app db data has changed, i have to deleteAllRows and draw up the tableview again in the enterscene event. Should i skip the createscene event all together and create the tableview in the enterscene event?

The app db will be updated in a synchronized timer event if it is changed serverside. No data from the app will updated the server db, that happens from other sources. First i was think about building the tableview with a directcall to the server, but i dont want the user to get a loadingscreen everytime except for when the app i launched.

Is this the right way to go, or should i think in a different manner? 

Thinking sounds good except you could create the tableView in creatScene event. Then in willEnterScene you can run deleteAll + insert rows. That way you get fresh tableview on each enterScene without seeing the table populate + don’t have to worry about destroying and re-creating tableviews.

If you have a lot of rows, you could have a minimum cache time as well, make a scene local variable with os.time() when the tableview was populated, and only refresh contents on each willEnterScene if appropriate time has passed. 

This worked perfectly :slight_smile: Thank you

This is the code for the tableView by the way. 

local function tableViewListener( event ) local phase = event.phase local row = event.target print( event.phase ) end -- Handle row rendering local function onRowRender( event ) local phase = event.phase local row = event.row local rowTitle = display.newText( row, " \> " .. data[row.index].Descr, 0, 0, nil, 14 ) rowTitle.x = row.x - ( row.contentWidth \* 0.5 ) + ( rowTitle.contentWidth \* 0.5 ) rowTitle.y = row.contentHeight \* 0.5 rowTitle:setTextColor( 0, 0, 0 ) end -- Handle touches on the row local function onRowTouch( event ) local phase = event.phase if "press" == phase then print( "Touched row:", event.target.index ) end end -- Create a tableView local tableView = widget.newTableView { top = 100, width = 320, height = 366, maskFile = "assets/mask-320x366.png", listener = tableViewListener, onRowRender = onRowRender, onRowTouch = onRowTouch, hideBackground = true, } group:insert( tableView ) --CREATE TABLEVIEW ROWS local function setupTableViewRows() for i = 1, numberOfRows do local isCategory = false local rowHeight = 40 local rowColor = { default = { 255, 255, 255 }, } local lineColor = { 150, 220, 220 } -- Make some rows categories if i == 25 or i == 50 or i == 75 then isCategory = true rowHeight = 24 rowColor = { default = { 150, 160, 180, 200 }, } end -- Insert the row into the tableView tableView:insertRow { isCategory = isCategory, rowHeight = rowHeight, rowColor = rowColor, lineColor = lineColor, } end native.setActivityIndicator( false ) end

If you want to re-render everything you can use the deleteAllRows function and then insert again as usual.  http://docs.coronalabs.com/daily/api/type/TableViewWidget/deleteAllRows.html

Im struggling with how the dataflows should look like. The app will probably work like this.

When the app launches, it connect to the php server function and request data. The data is stored in an app db.

When the user enters the tableview scene the tableview is built up from rows in the app db. 

If the user leave the scene, but it stays in the memory so when the user enters again the createscene will never fire. In this case if the app db data has changed, i have to deleteAllRows and draw up the tableview again in the enterscene event. Should i skip the createscene event all together and create the tableview in the enterscene event?

The app db will be updated in a synchronized timer event if it is changed serverside. No data from the app will updated the server db, that happens from other sources. First i was think about building the tableview with a directcall to the server, but i dont want the user to get a loadingscreen everytime except for when the app i launched.

Is this the right way to go, or should i think in a different manner? 

Thinking sounds good except you could create the tableView in creatScene event. Then in willEnterScene you can run deleteAll + insert rows. That way you get fresh tableview on each enterScene without seeing the table populate + don’t have to worry about destroying and re-creating tableviews.

If you have a lot of rows, you could have a minimum cache time as well, make a scene local variable with os.time() when the tableview was populated, and only refresh contents on each willEnterScene if appropriate time has passed. 

This worked perfectly :slight_smile: Thank you