Last active
December 15, 2015 12:19
-
-
Save miconblog/5259687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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(); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
간단 사용법
var dbupdator = require("dbupdator");
dbupdator.reload(function() {
// todo after update
});