What is the correct/best approach to get data from a database for something like calendar and its events. I think there are two possible ways:
从日历及其事件等数据库中获取数据的正确/最佳方法是什么。我认为有两种可能的方法:
1) Should I get all data from the database and save it in a $scope object, so that I access the SQLite db only once and work with the object later on
1)我是否应该从数据库中获取所有数据并将其保存在$ scope对象中,以便我只访问SQLite数据库一次并稍后使用该对象
2) Should I only get the data for the currently selected day of the calendar, and if I want to display another day, I do another sql query.
2)我应该只获取当前所选日期的数据,如果我想显示另一天,我会进行另一个SQL查询。
EDIT: the database is local, so network perfomance is no matter
编辑:数据库是本地的,因此网络性能无关紧要
1 个解决方案
#1
0
I think it depends on what is more important for you and how big the data is.
我认为这取决于对您来说更重要的是什么以及数据有多大。
If you are working with a local database you have keep in mind that you always have to do async operations to get data from your database. So you always have a little time (depending on the device performance) where the promises have to get resolved even if its on local db.
如果您正在使用本地数据库,请记住,您始终必须执行异步操作才能从数据库中获取数据。所以你总是有一点时间(取决于设备性能)承诺必须得到解决,即使它在本地数据库上。
My ionic application follows the concept 2 of your options without using any cache. The trick to render correct data is to resolve relevant data before entering the view.
我的离子应用程序遵循您的选项的概念2而不使用任何缓存。呈现正确数据的技巧是在进入视图之前解析相关数据。
My stateprovider looks like this:
我的stateprovider看起来像这样:
.state('app.messages', {
url: "/messages",
views: {
'menuContent': {
templateUrl: "pathToTemplate.html",
controller: 'ExampleMessageListCtrl'
}
},
resolve: {
messages: function($q,DatabaseService) {
var deferred = $q.defer();
//All db functions are async operations
DatabaseService.getMessageList().then(function (messageList) {
deferred.resolve(messageList);
}, function (err) {
deferred.reject(err);
});
return deferred.promise;
}
},
//Disable cache to always get the values from db
cache:false
In the controller you can access the messages
variable via injection:
在控制器中,您可以通过注入访问消息变量:
.controller('ExampleMessageListCtrl', function ($scope,messages) {
var loadMessages = function() {
$scope.messages = messages;
};
...
});
The benefits of this concept with resolving the data before entering the state is that you always get the data which is inside the db without rendering frames with empty data inside, which comes from default data inside the scope variables.
在进入状态之前解析数据这一概念的好处是,您始终可以获取数据库内部的数据,而不会在内部呈现空数据的帧,这些数据来自范围变量中的默认数据。
I think options 1 is the right option if you want to display data very quickly. The challenge in this case is to hold your cached data synchronized with the data in the database without loosing much performance, which I think is not very easy.
我认为如果你想快速显示数据,选项1是正确的选择。在这种情况下的挑战是保持您的缓存数据与数据库中的数据同步,而不会失去太多的性能,我认为这不是很容易。
#1
0
I think it depends on what is more important for you and how big the data is.
我认为这取决于对您来说更重要的是什么以及数据有多大。
If you are working with a local database you have keep in mind that you always have to do async operations to get data from your database. So you always have a little time (depending on the device performance) where the promises have to get resolved even if its on local db.
如果您正在使用本地数据库,请记住,您始终必须执行异步操作才能从数据库中获取数据。所以你总是有一点时间(取决于设备性能)承诺必须得到解决,即使它在本地数据库上。
My ionic application follows the concept 2 of your options without using any cache. The trick to render correct data is to resolve relevant data before entering the view.
我的离子应用程序遵循您的选项的概念2而不使用任何缓存。呈现正确数据的技巧是在进入视图之前解析相关数据。
My stateprovider looks like this:
我的stateprovider看起来像这样:
.state('app.messages', {
url: "/messages",
views: {
'menuContent': {
templateUrl: "pathToTemplate.html",
controller: 'ExampleMessageListCtrl'
}
},
resolve: {
messages: function($q,DatabaseService) {
var deferred = $q.defer();
//All db functions are async operations
DatabaseService.getMessageList().then(function (messageList) {
deferred.resolve(messageList);
}, function (err) {
deferred.reject(err);
});
return deferred.promise;
}
},
//Disable cache to always get the values from db
cache:false
In the controller you can access the messages
variable via injection:
在控制器中,您可以通过注入访问消息变量:
.controller('ExampleMessageListCtrl', function ($scope,messages) {
var loadMessages = function() {
$scope.messages = messages;
};
...
});
The benefits of this concept with resolving the data before entering the state is that you always get the data which is inside the db without rendering frames with empty data inside, which comes from default data inside the scope variables.
在进入状态之前解析数据这一概念的好处是,您始终可以获取数据库内部的数据,而不会在内部呈现空数据的帧,这些数据来自范围变量中的默认数据。
I think options 1 is the right option if you want to display data very quickly. The challenge in this case is to hold your cached data synchronized with the data in the database without loosing much performance, which I think is not very easy.
我认为如果你想快速显示数据,选项1是正确的选择。在这种情况下的挑战是保持您的缓存数据与数据库中的数据同步,而不会失去太多的性能,我认为这不是很容易。