Skip to content

Instantly share code, notes, and snippets.

@miconblog
Last active December 15, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miconblog/5259687 to your computer and use it in GitHub Desktop.
Save miconblog/5259687 to your computer and use it in GitHub Desktop.
(function() {
var setUpIndicator = function(){
console.log("새로운 DB를 다운로드합니다.");
};
var setDownIndicator = function(){
console.log("데이터베이스를 완료!!");
};
exports.reload = function(callback) {
var filename = "_alloy_.sql", // 앱에서 관리하는 DB 파일 이름
url = "http://remote_site/_database_.sqlite", // 웹에서 관리하는 파일
file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory),
privateDir = file.nativePath.replace("Documents/", "Library/Private%20Documents/");
if (Titanium.Network.online) {
setUpIndicator();
var http = Titanium.Network.createHTTPClient();
http.setTimeout(10000);
http.onload = function() {
if (http.status == 200) {
// 앱안에 인스톨된 DB가 있으면 지운다.
file = Titanium.Filesystem.getFile(privateDir, filename);
if (file.exists()) {
file.deleteFile();
}
// 새로운 DB를 설치한다.
var f = Titanium.Filesystem.getFile(privateDir, filename);
f.write(this.responseData);
console.log("완료!", this.responseData);
setDownIndicator();
callback();
} else {
console.log("다운로드중... 에러 발생~!!");
setDownIndicator();
callback();
}
};
http.ondatastream = function(e) {
console.log("다운로드중... ", e.progress);
};
http.onerror = function(e) {
setDownIndicator();
console.log("에러... ", e.error);
};
http.open('GET', url);
http.send();
} else {
console.log("에러... 인터넷 연결 안됨.");
callback();
}
};
})();
@miconblog
Copy link
Author

간단 사용법
var dbupdator = require("dbupdator");
dbupdator.reload(function() {
// todo after update
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment