Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 21
Registered: ‎05-28-2013
My Device: Z10
My Carrier: Telus

Failed to save ArrayDataModel with JsonDataAccess

I am trying to save an arraydatamodel with QVariantMap with JsonDataAccess to file and load it back, however, it's saving 103 bytes to file, but when I try to load it, it will always come up as NULL.

 

// Default empty project template
#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/data/JsonDataAccess>
#include "CountdownTimer.hpp"

using namespace bb::cascades;

ApplicationUI::ApplicationUI(bb::cascades::Application *app)
: QObject(app)
{
    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument *qml = QmlDocument::create("asset:///main.qml")
    	.parent(this);

    // Initialize the Array Data Model for holding events
    //this->eventsModel = new ArrayDataModel();
    this->loadData();
    // For Testing only
//    QVariantMap event;
//    event["EventName"] = "Event1";
//    event["EventHour"] = "00";
//    event["EventMinute"] = "01";
//    event["EventSecond"] = "02";
//    this->eventsModel->append(event);

    // Setting the access for QML to member variable
    qml->setContextProperty("eventsModel",this->eventsModel);
    qml->setContextProperty("mainObj",this);

    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    // Registering the QTimer type to QML
    qmlRegisterType<CountdownTimer>("TimerLibrary", 1, 0, "CountdownTimer");

    // set created root object as a scene
    app->setScene(root);
}

void ApplicationUI::loadData()
{
	bb::data&colon;:JsonDataAccess jda;

	// Home dir in app/data
	QDir dirHome = QDir::home();
	QFile file(dirHome.absoluteFilePath("EventsModel.json"));

	if (file.exists()) {
		qDebug() << "Data file exists, continuing to load, file size : " << file.size();
		QVariant list = jda.load(dirHome.absoluteFilePath("EventsModel.json"));
		qDebug() << "Finished loading file to array" << list.toString();
		this->eventsModel = new ArrayDataModel(list.value<QVariantList>());
	}
	else {
		qDebug() << "Data file does not exist, initializing array";
		this->eventsModel = new ArrayDataModel();
	}

	// Insert the data into the data model. Because the root of the .json file
	// is an array, a QVariant(QVariantList) is returned from load(). You can
	// provide a QVariantList to a data model directly by using insertList().
	//eventsModel->insertList(list.value<QVariantList>());
}

void ApplicationUI::saveData()
{
	bb::data&colon;:JsonDataAccess jda;
	QDir dirHome = QDir::home();
	QFile file(dirHome.absoluteFilePath("EventsModel.json"));
	QList<QVariant> list;

	for (int i=0; i < this->eventsModel->size(); i++) {
		list << QVariant(this->eventsModel->value(i));
		//		jda.save(this->eventsModel->value(i), &file);
	}
	jda.save(QVariant(list), &file);
	qDebug() << "File size : " << file.size();
}

.....

 

Thanks