我正在尝试完成以下任务。我有一个从远程数据库返回的json数组,我想迭代它,检查本地数据库中是否存在具有该对象ID的记录。如果存在,我想更新记录,如果不存在我想要附加它。代码如下:
$.each(data, function(idx, task) {
var taskToUpdate = $org.context.Task.attachOrGet({ Id:task.TaskId});
taskToUpdate.TaskType = task.TaskType;
taskToUpdate.StatusId = task.TaskStatusId;
taskToUpdate.TaskStatus = task.TaskStatus;
taskToUpdate.DateScheduled = task.Date;
taskToUpdate.TimeSlot = task.Time;
taskToUpdate.LastUpdated = new Date();
taskToUpdate.TaskName = "Job " + task.TaskId + " " + task.TaskType + " @" + task.AddressOfTask + ", " + task.PropertyPostCode;
taskToUpdate.SpecialInstructions = task.SpecialInstructions;
taskToUpdate.PropertyAddress = task.AddressOfTask;
taskToUpdate.PropertyPostCode = task.PropertyPostCode;
taskToUpdate.PropertyType = task.PropertyType;
taskToUpdate.NumberOfBedrooms = task.NumberOfBedrooms;
taskToUpdate.HasGarage = task.HasGarage;
taskToUpdate.HasOutHouse = task.HasOutHouse;
});
$org.context.saveChanges({
success: function(db) {
that.messages.push("Tasks saved to local device.");
}, error: function(err) {
console.log(err);
that.messages.push("Errors saving tasks: " + err);
navigator.notification.alert("Error saving local tasks to your device!",
function () {
}, "Error", 'OK');
}
});
代码执行成功,但没有记录添加到任务表中。
我错过了什么吗?
答案 0 :(得分:1)
我使用了这段代码,这似乎有效,但感觉并不“正确”。即我检测更新是否已完成的方式,以避免调用context.savechanges()多个。请随时改进我的答案!
function downloadTasksFromWeb(viewModel){
$org.context.UserSetting.first().then(function (userSetting) {
viewModel.set("currentUserSettings", userSetting);
backofficeUrl = viewModel.get("currentUserSettings.BackOfficeUrl") + "/api/tasks";
var operatorId = viewModel.get("currentUserSettings.OperatorId");
var rowsToProcess = 0, rowsProcessed = 0;
viewModel.messages.push("Connecting to server.");
showNotificationInfo("Connecting to server.");
jQuery.ajax({
type: "GET",
url: backofficeUrl,
dataType: 'json',
async: false,
username: "user",
password: "pw",
data: {"operatorId": operatorId},
success: function (data) {
viewModel.messages.push("Tasks received, saving to local device.");
showNotificationInfo("Tasks received, saving to local device.");
rowsToProcess = data.length;
$.each(data, function(idx, task) {
var existingTasks = $org.context.Task.filter("Id", "==", task.TaskId).toArray();
existingTasks.then(function(result) {
var taskToUpdate = $org.context.Task.attachOrGet({ Id:task.TaskId});
taskToUpdate.TaskType = task.TaskType;
taskToUpdate.StatusId = task.TaskStatusId;
taskToUpdate.TaskStatus = task.TaskStatus;
taskToUpdate.DateScheduled = task.Date;
taskToUpdate.TimeSlot = task.Time;
taskToUpdate.LastUpdated = new Date();
taskToUpdate.TaskName = "Job " + task.TaskId + " " + task.TaskType + " @" + task.AddressOfTask + ", " + task.PropertyPostCode;
taskToUpdate.SpecialInstructions = task.SpecialInstructions;
taskToUpdate.PropertyAddress = task.AddressOfTask;
taskToUpdate.PropertyPostCode = task.PropertyPostCode;
taskToUpdate.PropertyType = task.PropertyType;
taskToUpdate.NumberOfBedrooms = task.NumberOfBedrooms;
taskToUpdate.HasGarage = task.HasGarage;
taskToUpdate.HasOutHouse = task.HasOutHouse;
if (result.length == 0) {
$org.context.Task.add(taskToUpdate);
}
rowsProcessed++;
if (rowsProcessed == rowsToProcess) {
$org.context.saveChanges({
success: function(db) {
viewModel.messages.push("Tasks saved to local device.");
showNotificationInfo("Tasks saved to local device.");
}, error: function(err) {
console.log(err);
viewModel.messages.push("Errors saving tasks: " + err);
showNotificationError("Errors saving tasks: " + err);
}
});
}
});
});
}
}).fail(function(resultData) {
showNotificationError("There was an error communicating with the server. Please check your settings and try again.");
});
});
}
答案 1 :(得分:0)
使用以下算法,您需要一个递归函数,而不是$ .each: - 检查是否有任何记录保存到具有特定ID的本地DB
ontext.Tasks.add(newEntity)
context.saveChanges(
)以保留批量中的所有更改。这比调用foreach中的saveChanes()的代码快得多,也更安全。由于异步行为,递归函数是必需的。