Posted By

pawaca on 03/02/09


Tagged


Versions (?)

[Workaround] AIR randomly crash on Mac


 / Published in: MXML
 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
  3. <mx:Script>
  4. <![CDATA[
  5. import mx.utils.ObjectUtil;
  6.  
  7. private namespace safe;
  8. private namespace normal;
  9.  
  10. private function init():void
  11. {
  12. use namespace safe;
  13. //use namespace normal; //如果把 namespace 换成 normal 在 mac 上会造成 crash
  14.  
  15. var dbFile:File = new File("app-storage:/test.db");
  16. if(dbFile.exists)
  17. {
  18. dbFile.deleteFile();
  19. }
  20. var connection:SQLConnection = new SQLConnection();
  21. connection.open(dbFile);
  22.  
  23. connection.begin();
  24. executeSql(connection,"CREATE TABLE foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar0 OBJECT NOT NULL, bar1 OBJECT NOT NULL);");
  25. connection.commit();
  26.  
  27. //喂点复杂的 object graph 给 sqlite...
  28. var obj0:Object = {prop0:"blah"};
  29. var obj1:Object = {prop0:obj0, prop1:"test"};
  30. var obj2:Object = {prop0:obj1, prop2: obj0};
  31.  
  32. connection.begin();
  33. executeSql(connection,"INSERT INTO foo (bar0, bar1) VALUES(:bar0, :bar1);",{bar0:obj1,bar1:obj2});
  34.  
  35. for(var i:int=0;i<1000;i++)
  36. {
  37. obj0.prop0=Math.random();
  38. executeSql(connection,"UPDATE foo SET bar0=:bar0, bar1=:bar1;",{bar0:obj1,bar1:obj2});
  39. executeSql(connection,"SELECT * FROM foo");
  40. }
  41.  
  42. connection.commit();
  43.  
  44. }
  45.  
  46. normal function executeSql(connection:SQLConnection,str:String,param:Object=null,mapping:Class=null):SQLResult
  47. {
  48. var s:SQLStatement = new SQLStatement();
  49. s.sqlConnection = connection;
  50. s.text = str;
  51. if(param!=null)
  52. {
  53. for(var k:String in param)
  54. {
  55. var p:Object = param[k];
  56. s.parameters[":"+k] = p;
  57. }
  58. }
  59. if(mapping!=null)
  60. {
  61. s.itemClass = mapping;
  62. }
  63. s.execute();
  64. return s.getResult();
  65. }
  66.  
  67. safe function executeSql(connection:SQLConnection,str:String,param:Object=null,mapping:Class=null):SQLResult
  68. {
  69.  
  70. var s:SQLStatement = new SQLStatement();
  71. s.sqlConnection = connection;
  72. s.text = str;
  73.  
  74. if(param!=null)
  75. {
  76. for(var k:String in param)
  77. {
  78. var p:Object = param[k];
  79. if(!isPlainType(p))
  80. {//将 复杂 Object 先 write 到 byteArray 里
  81. p = objectToByteArray(p);
  82. }
  83. s.parameters[":"+k] = p;
  84. }
  85. }
  86.  
  87. if(mapping!=null)
  88. {
  89. s.itemClass = mapping;
  90. }
  91. s.execute();
  92.  
  93. var result:SQLResult = s.getResult();
  94. if(result.data!=null)
  95. {
  96. var c:int = result.data.length;
  97. for(var i:int=0;i<c;i++)
  98. {
  99. var row:Object = result.data[i];
  100. for(var j:String in row)
  101. {
  102. var val:Object = row[j];
  103. if(val is ByteArray)
  104. {// 还原 byteArray 里面的 object
  105. val = objectFromByteArray(val as ByteArray);
  106. row[j]=val;
  107. }
  108. }
  109. }
  110. }
  111. return result;
  112. }
  113.  
  114. private function isPlainType(obj:Object):Boolean
  115. {
  116. var type:String = typeof(obj);
  117. switch (type)
  118. {
  119. case "number":
  120. case "string":
  121. case "boolean":
  122. {
  123. return true;
  124. }
  125. case "object":
  126. {
  127. return (obj is Date);
  128. }
  129. }
  130. return false;
  131. }
  132.  
  133. private function objectToByteArray(obj:Object):ByteArray
  134. {
  135. var bytes:ByteArray = new ByteArray();
  136. bytes.writeObject(obj);
  137. bytes.position=0;
  138. return bytes;
  139. }
  140.  
  141. private function objectFromByteArray(bytes:ByteArray):Object
  142. {
  143. return bytes.readObject();
  144. }
  145. ]]>
  146. </mx:Script>
  147. </mx:WindowedApplication>

Report this snippet  

You need to login to post a comment.