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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Highlighted
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