跟踪在不同线程上对NSManagedObject所做的更改

时间:2015-04-22 16:22:49

标签: multithreading core-data

我想将对任何<html> <head> <style type="text/css"> h1{ width:100%; background-color:maroon; color:white; } fieldset{ width:500px; margin:auto; } </style> <script> function checkAnswers(){ var inputs = document.querySelectorAll('input[type="radio"]'); var check = 0; var score = 0,i=0; for(i;i<inputs.length;i++){ var inputName = inputs[i].getAttribute("name"); var checker = document.getElementsByName(inputName); var counter = false; for(j=0;j<checker.length;j++){ if(checker[j].checked == true){ counter = true; i += 4; break; } } if(counter == false){ alert("Must answer question number" + " " + inputName + "!"); document.getElementById('HG-' + inputName).style.backgroundColor = "red"; i += 4; check++; } else{ document.getElementById('HG-' + inputName).style.backgroundColor = "white"; } } if(check == 0){ for(i=0;i<inputs.length;i++){ var inputName = inputs[i].getAttribute('name'); var msg = document.getElementById('msg-' + inputName); var store; if(inputs[i].value == "yes" && inputs[i].checked == true){ msg.innerHTML = 'Correct Answer!'; document.getElementById('msg-' + inputName).style.color = "green"; document.getElementById('HG-' + inputName).style.backgroundColor = "white"; score++; } else if(inputs[i].value == "no" && inputs[i].checked == true){ msg.innerHTML = 'Wrong Answer!'; document.getElementById('msg-' + inputName).style.color = "red"; document.getElementById('hg-' + inputName).style.backgroundColor = "yellow"; document.getElementById('HG-' + inputName).style.backgroundColor = "white"; } } document.getElementById("score").innerHTML="Your score is:" + score; return false; } return false; } </script> </head> <body> <form name="myForm" onsubmit="return checkAnswers()" id="form"> <center><h1>QUESTIONS</h1></center> <fieldset><br /> <div id="HG-one"> <span id="msg-one"></span> <span>1.) It is a simple design language intended to simplify the process of maing web pages presentable.</span><br /> </div><br /> <input name="one" type="radio" value="yes" /><span id="hg-one">A. CSS</span><br /> <input name="one" type="radio" value="no" />B. HTML <br /> <input name="one" type="radio" value="no" />C. XML <br /> <input name="one" type="radio" value="no" />D. Bootstrap <br /><br /> </fieldset> <br /><br /> <fieldset><br /> <div id="HG-two"> <span id="msg-two"></span> <span>2.) Where is the Eiffel Tower located?</span><br /></div><br /> <input name="two" type="radio" value="no" />A. Miag ao<br /> <input name="two" type="radio" value="yes" /><span id="hg-two">B. Paris</span><br /> <input name="two" type="radio" value="no" />C. Madrid<br /> <input name="two" type="radio" value="no" />D. Korea<br /> </fieldset> <br /><br /> <fieldset><br /> <div id="HG-three"> <span id="msg-three"></span> <span>3.) Which country produces Proton cars?</span><br /></div><br /> <input name="three" type="radio" value="no" />A. Indonesia <br /> <input name="three" type="radio" value="yes" /><span id="hg-three">Malaysia</span><br /> <input name="three" type="radio" value="no" />C. Japan<br /> <input name="three" type="radio" value="no" />D. South Korea<br /> </fieldset> <br /><br /> <fieldset><br /> <div id="HG-four"> <span id="msg-four"></span> <span>4.) What is the name of the tallest structure ever built?</span><br /></div><br /> <input name="four" type="radio" value="no" />A. Grollo Tower <br /> <input name="four" type="radio" value="no" />B. CN Tower<br /> <input name="four" type="radio" value="yes" /><span id="hg-four">C. Burj Khalifa</span><br /> <input name="four" type="radio" value="no" />D. Taipei 101<br /> </fieldset> <br /><br /> <fieldset><br /> <div id="HG-five"> <span id="msg-five"></span> <span>5.) What type of creature is the children's animated character Pingu?</span><br /></div><br /> <input name="five" type="radio" value="no" />A. Porcupine <br /> <input name="five" type="radio" value="no" />B. Pony<br /> <input name="five" type="radio" value="no" />C. Pelican<br /> <input name="five" type="radio" value="yes" /><span id="hg-five">D. Penguin</span> <br /> </fieldset> <br /><br /> <fieldset><br /> <div id="HG-six"> <span id="msg-six"></span> <span>6.) What is the longest river in Pakistan?</span><br /></div><br /> <input name="six" type="radio" value="no" />A. Incara <br /> <input name="six" type="radio" value="no" />B. Indeema<br /> <input name="six" type="radio" value="no" />C. Indira<br /> <input name="six" type="radio" value="yes" /><span id="hg-six">D. Indus</span> <br /> </fieldset> <br /><br /> <fieldset><br /> <div id="HG-seven"> <span id="msg-seven"></span> <span>6.) What is the longest river in Pakistan?</span><br /></div><br /> <input name="seven" type="radio" value="no" />A. Incara <br /> <input name="seven" type="radio" value="no" />B. Indeema<br /> <input name="seven" type="radio" value="no" />C. Indira<br /> <input name="seven" type="radio" value="yes" /><span id="hg-seven">D. Indus</span> <br /> </fieldset> <center><input type="submit" name='submit' value="Submit" /><br /><br /> <span id="score"></span><br /> </form> </body> </html> 所做的更改发送到服务器,我想在后台执行此操作。

因此,我想设置一个后台线程,其自己的NSManagedObject注册NSManagedObjectContext。但是,我无法访问NSManagedObjectContextDidSaveNotification changedValues上的userInfo,因为它位于不同的主题上。

那怎么办呢?

1 个答案:

答案 0 :(得分:0)

简单方法是:不要打扰changedValues。只需发送任何更改的托管对象的所有属性。开销通常不是很大,而且您的代码会更简单。

如果您确实只想发送已更改的属性值,则会变得更复杂:

  • 可以使用changedValues,只要您使用NSMainQueueConfinementNSPrivateQueueConfinement ,就可以致电{{1}使用changedValuesperformBlock:使用获取更改的块。获取更改的值并将其复制到Core Data之外的内容。确保对Core Data中的任何内容的所有访问都在其中一个块中。但是保存后performBlockAndWait:没用,所以......
  • 如果您使用changedValues,则需要使用changedValues来获取有关更改的通知,但这可能会发生很多,所以......
  • 最好使用NSManagedObjectContextObjectsDidChangeNotification,这只会在您实际保存更改时才会发生。没有NSManagedObjectContextWillSaveNotification,因此您必须查看通知userInfo(这将是object),然后使用NSManagedObjectContextinsertedObjects和{ {1}}找出哪些对象已更改。

有了这个,您可以访问更改的值,将它们复制到您用来保存传出更改的任何数据结构,然后发送它们。

但如果可能的话,我会忽略updatedObjects