Titanium android data reflection in table view - titanium

Google APIs Android 2.3.1 , data changes in the database tables not reflecting in tableview screens, but if we restart the app, will show changes and also, if we load app.js will show recent changes.
Can anyone help me to fix this??
fireEvent("db_update")
&
addEventListener('db_update', function() { what should be here to update table data })

For updating a database, you need to remove it first, and then install a new one. Or run queries to update the current one.
This is how I update my database, based on a version number I store in the properties:
function updateDatabase(version){
if (version != Ti.App.Properties.getInt('version',0)){
sb.db.remove();
sb.db = Ti.Database.install('/lib/db.sqlite','db');
Ti.App.Properties.setInt('version',version);
}
}
In the app, on start, I run the function with a manual typed version number in it. Every time your database changes you can manually change this number.
updateDatabase(5);
This of course does not reflect a regular update on the content of a table. (when you run a regular query on the database).
In that case, you should rebuild the content of the tableview yourself (you have the code to fill the tableview, rerun that code) or find a way to target the exact row you need to change with an ID for example.

I found the solution,
Ti.App.addEventListener('updatedb', updateData);
here 'updateData' is the function which gets the data for tableview from the databae. At the
bottom of 'updateData' function, set data for table view
i.e) tableview.setData(data);
and then, your insert, update or delete function should trigger 'updatedb' function
i.e)
function deleteData(recId) {
var db = Ti.Database.open(DBNAME);
db.execute(" DELETE FROM tblName WHERE id IN("+ recId+") ");
db.close();
Ti.App.fireEvent('dataupdated');
}
now, after calling deleteData function, your tableview data will show w/o deleted record.

Related

ATK4: Handling many to many relationships in CRUD - Adding subCRUD without breaking Add New

I'm trying to gracefully handle extra many to many relationship data in an ATK4 CRUD object. I can manually add the current entry's linked tables as sub CRUD objects while isEditing(), but in doing so i break the 'Add New' button. Code as follows:
function page_projects($p) {
$crud = $this->add('CRUD');
$crud->setModel('Project', null, array('Title', 'School'));
if($crud->grid){
$crud->grid->addPaginator(10);
$crud->grid->addQuicksearch(array('Title','School'));
}
if($crud->isEditing()){
$vForm = $crud->form;
$keywords = $vForm->add('CRUD');
$keywords->entity_name = 'Keyword';
$keywords->setModel(
$crud->model->load($crud->id)->
ref("mProjectKeywords"));
//Other snipped m:m rels
}
}
The issue is simply that $crud->id is not populated (and shouldn't be) when generating a new entry, which means i can't spawn new sub cruds. A workaround is to use:
if($crud->isEditing() && !is_null($crud->id)) {
when checking isEditing(), but this simply stops the sub CRUDS from being instantiated to avoid having load() throw an exception. I've tried looking at loadAny() and tryLoad() and neither do what i want in a many to many context: load a record if one exists, generate a new one otherwise.
Does anyone know a better way of handling this, and if there's not already one in the framework then what's the best angle for attacking this problem?
In this case that's normal behavior because you simply can't create mProjectKeywords record in database table before you INSERT referenced records in parent tables (Project and Keywords).
I do something like if($crud->isEditing() && !is_null($crud->id)) in my projects too.
That means you first have to save (insert) new record in Project and only then sub-CRUD shows up and you can add referenced records in Project edit form (which reloads automatically after you save new project).

Yii CacheDependency based on cache value

This seems like a simple thing and should be part of the base code for Yii, but I can't find a solution anywhere. Here is my scenario.
1) User updates their record (use beforesave to set a cache value, changes with each new save, php unique())
public function beforeSave()
{
Yii::app()->cache->set('userupdate'.$this->id,uniqid());
return parent::beforeSave();
}
2) User data is cached using the cache value in step one as a dependency in the loadModel function of the model.
$model=Users::model()->cache(1800, $dependency)->findByPk($id);
3) User views a page that calls to retrieve their data. Yii evaluates the request to see if the cached valued from step 1 has changed, if it has not pull from cache, if it has pull from db.
While reading this page (http://www.yiiframework.com/doc/guide/1.1/en/caching.data) it has that function if a file date changes, but not one for it a variable changes. Any help in this matter would be great as I am at a loss of how to implement this.
NOTE: I need to use cache to hold the variable as I'm running multiple instances of my application and they need shared over each server and all users (thus session won't work).
After fighting with this I found the solution, don't feel it's completely pretty, but it does work. Any feedback on a cleaner way is much appreciated.
$cache = Yii::app()->cache;
$key1 = 'userupdate'.$id; //main cache value
$key2 = '2userupdate'.$id; //will equal main cache when query is cached
$cache1 = $cache['userupdate'.$id];
$cache2 = $cache['2userupdate'.$id];
$dependency = new CExpressionDependency("Yii::app()->cache->get('$key1') == Yii::app()->cache->get('$key2')");
$model=Users::model()->cache(1800,$dependency)->findByPk($id);
if($cache1 != $cache2)
$cache['2userupdate'.$id] = $cache['userupdate'.$id];
One of the dependency options is CExpressionDependency. You could compare the currently cached beforeSave value to the value you get from the loadModel call.

WCF Data Service - update a record instead of inserting it

I'm developing a WCF Data Service with self tracking entities and I want to prevent clients from inserting duplicated content. Whenever they POST data without providing a value for the data key, I have to execute some logic to determine whether that data is already present inside my database or not. I've written a Change interceptor like this:
[ChangeInterceptor("MyEntity")]
public void OnChangeEntity(MyEntity item, UpdateOperations operations){
if (operations == UpdateOperations.Add)
{
// Here I search the database to see if a matching record exists.
// If a record is found, I'd like to use its ID and basically change an insertion
// into an update.
item.EntityID = existingEntityID;
item.MarkAsModified();
}
}
However, this is not working. The existingEntityID is ignored and, as a result, the record is always inserted, never updated. Is it even possible to do? Thanks in advance.
Hooray! I managed to do it.
item.EntityID = existingEntityID;
this.CurrentDataSource.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
I had to change the object state elsewhere, ie. by calling .ChangeObjectState of the ObjectStateManager, which is a property of the underlying EntityContext. I was mislead by the .MarkAsModified() method which, at this point, I'm not sure what it does.

NHibernate refusing to Refresh an object

We are running webservices which get called to populate and update our database, in one of the functions an EntityReactionType get's filled by the caller of the webservice with the ID's.
EntityReactionType has a composite many-to-one key consisting of Entity and (suprise) ReactionType.
These ID's get filled with the right values and i then want to refresh the object from the database in order to check some values which are set in the database but the user has no knowledge of. In this case: IsStart and IsClosing.
I do all this with the following code: (it's a simplified snipped but the idea should be clear)
I can see the refresh executing the SQL, which is correct and shows that this has IsStart = False and IsClosing = True.
Yet when i look at the object values after the Refresh IsStart = null and IsClosing = False;
//entityReactionTypeRepository.Merge(reaction.EntityReactionType);
entityReactionTypeRepository.Refresh(reaction.EntityReactionType);
if (reaction.IsResolved == true || reaction.EntityReactionType.CloseDispute == true)
{
reaction.IsResolved = true;
reaction.Invoice.IsDisputed = false;
}
I have the Merge line commented out since that is something i found while searching google but changed nothing (idea was that you can't refresh something that isn't transistant but nothing changed, SQL get's executed eitherway but the values do not get updated)
I'm close to making a work around at this time that just creates a detachedcriteria and executes that but i know this should be possible like this.
Greetings,
Folkert
Well today i revisited this problem and i think i understand why it did this, it seems to me when i called the Refresh it kept the original values because in my code before i refresh i also made a Invoice object presistant. The invoice itself doesn't have EntityReactionTypes but in it's object graph it does reference there.
I figure for this reason the session decides to keep the values as they are in the presistent version of the invoice.
So what i did is i put this refresh pretty much at the top of the method and it started to work like it used to and actually pull the values from the database.
Seems odd to me though that it wouldn't change the property values when i did a refresh but this is what it did for me.

Unit testing NHibernate application with SQLite: it writes to the database but cannot read back

I have an application using NHibernate that is already deployed and working properly, and I'm re-factoring the unit tests to use SQLite for improved performance, and to keep unit test data out of the "real" database.
I have a simple test that creates an Calendar entity, saves it, then tries to read it back and verifies that it's the same object. The write works, but the subsequent select to read it back returns 0 records. A Calendar has a GUID as a primary key, and I understand that requires an extra parameter on the SQLite connection string. This is my connection string:
data source=:memory:;Version=3;New=true;Pooling=true;Max Pool Size=1;BinaryGuid=False
Through the logged SQL statements coming from NHibernate, I see the inserts to write the entity and its dependencies, then the subsequent select statement. It all looks good, but nothing is selected. If I use a file database instead of an in-memory database, I can open up the table in Visual Studio's Server Explorer, and I see the correct data in the tables. If I write a query to try selecting the record, like so:
SELECT CalendarID, Name, Description
FROM dbo_Calendars
WHERE (CalendarID = 'a9cd9820-1694-4645-88d4-f682c5a6b9cc')
it also fails to select anything. I think it's an issue with GUID handling, but I'm flummoxed.
Update
Here's what the test case looks like:
[Test]
public void SaveAndLoadCalendar()
{
Guid calId;
DAOFactory factory = (DAOFactory)DAOFactory;
ISession s = factory.SessionManager.CurrentSession;
using (var tx = s.BeginTransaction())
{
Calendar cal = new Calendar("Test Calendar", CalendarType.Test);
cal.Active = true;
cal.Browsable = true;
s.Save(cal);
tx.Commit();
calId = cal.ID;
}
Logger.InfoFormat("Calendar ID is {0} ", calId);
s.Clear();
using (var tx2 = s.BeginTransaction())
{
Calendar cal = s.Get<Calendar>(calId);
Assert.IsNotNull(cal, "Could not retrieve saved calendar");
Assert.AreEqual("Test Calendar", cal.Name, "Saved calendar not equal to original calendar");
}
}
I would guess that the transaction handling could be the problem.
So maybe the transaction inserting the record is not yet committed and so the (different) transaction performing the select does not yet see the new data - so the select returns nothing.
I figured it out, and the problem isn't NHibernate or SQLite, it's me. Each Calendar has an associated Theme. In our production database, these are manually entered, and expected to exist in advance. Now that I'm using SQLite for testing, I'm starting with an empty database, and this reference data isn't pre-populated. NHibernate's Select statement to fetch the Calendar uses an inner join on the Themes table, and with nothing in that table, the select will return empty. D'oh.
After updating my test setup code to save the default theme, the test passes.