mat*_*mat 1 microsoft-metro winjs windows-store-apps
我已经为Windows应用商店构建了一个应用程序,它使用Windows.Storage.FileIO(createFileAsync,readTextAsync和writeTextAsync)写入本地文件.
我的计划是将文件用作简单的数据库(数据只是JSON格式的用户信息).这是一个坏主意吗?
以这种方式编写的文件是否真的持久(或者在更新应用程序时是否删除)?用户可以自己删除它们(例如通过删除cookie /临时网络文件等)吗?
我只是想知道应用程序本地写入磁盘的文件是如何被"Metro"环境处理的......
dev*_*mer 11
这是一个"坏主意"的问题是非常主观的,所以可能不是正确的场所.
但是,就Windows应用商店应用如何处理您存储的文件而言,如果使用Windows.Storage.ApplicationData.current.localFolder创建和/或编辑文件,这些文件将在用户卸载应用之前可用.如果您将应用程序的更新推送到商店,并且用户下载并安装更新,则文件将保留在用户的计算机上,除非您以编程方式明确地修改它们.
在我构建的两个应用程序中,我利用localFolder来存储应用程序数据的JSON表示,这样我可以最小化启动时间,并在应用程序启动后在后台刷新数据并向用户呈现其初始UI .
这是我的代码(可能有点优化)看起来像:
appData.current.localFolder.createFileAsync("leaderboard.txt",
Windows.Storage.CreationCollisionOption.openIfExists)
.then(function (file) {
leaderboardFile = file;
return Windows.Storage.FileIO.readTextAsync(file)
})
.then(function (fileContents) {
cachedLeaderboard = fileContents;
if (!cachedLeaderboard || isRefresh) {
return WinJS.xhr(xhrOptions)
}
else {
cachedLeaderboard = JSON.parse(cachedLeaderboard);
List = new WinJS.Binding.List(cachedLeaderboard);
completed(List);
}
})
.then(function (result) {
if (result) {
var items = JSON.parse(result.responseText).d;
localLeaderboard = items;
return Windows.Storage.FileIO.writeTextAsync(leaderboardFile,
JSON.stringify(localLeaderboard))
}
})
.then(function () {
if (!localLeaderboard)
localLeaderboard = cachedLeaderboard;
List = new WinJS.Binding.List(localLeaderboard);
completed(List);
})
Run Code Online (Sandbox Code Playgroud)
基本上,代码将检查leaderboard.txt文件是否存在(或者如果调用是刷新数据),如果不存在,则进行XHR调用以获取数据,然后创建绑定列表并将其传递回调用者包含在一个承诺中(未显示承诺代码).如果文件存在,并且请求不是刷新,我只需解析文本文件并从中创建绑定列表.
希望有所帮助!
归档时间: |
|
查看次数: |
3910 次 |
最近记录: |