nsmutablearray - NSArraym was mutated while being enumerated, sqlite3 while loop? -


i error: nsarraym mutated while being enumerated , i've tried find on stackoverflow. keep in mind i'm learning create game :)

i'm using sprite kit , uses dispatch_async in didmovetoview load game.

dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_default, 0), ^(void){        // loading stuff, causing error:        [self buildmap];     dispatch_async(dispatch_get_main_queue(), ^(void){         // done loading     }); }); 

here buildmap causes crash.

-(void)buildmap {      int intx = 0;     int inty = 0;      sqlite3_stmt *statement;     if (sqlite3_open([_databasepath utf8string], &blockminerdb) == sqlite_ok) {          nsstring *sql = [nsstring stringwithformat:@"select blocktype, ladder, blockreachable, walkable, zpos, texture, id, bitmask map order id desc"];         const char *qstmt = [sql utf8string];          if (sqlite3_prepare_v2(blockminerdb, qstmt, -1, &statement, null) == sqlite_ok) {              while (sqlite3_step(statement) == sqlite_row) {                  int blocktype = sqlite3_column_int(statement, 0);                 int ladder = sqlite3_column_int(statement, 1);                 int blockreachable = sqlite3_column_int(statement, 2);                 int walkable = sqlite3_column_int(statement, 3);                 int zpos = sqlite3_column_int(statement, 4);                 nsstring *texture = [nsstring stringwithutf8string:(char*)sqlite3_column_text(statement, 5)];                 int idblock = sqlite3_column_int(statement, 6);                 uint32_t newbitmask = (uint32_t)[nsstring stringwithutf8string:(char*)sqlite3_column_text(statement, 7)];                    blocks *testblock = [[blocks alloc] initwithblock:blocktype blockid:idblock ladder:ladder scene:self bitmask:newbitmask blockreachable:blockreachable walkable:walkable zpos:zpos texture:texture x:intx y:inty];                  nslog(@"%@: %i", testblock.name, blocktype);                  if ((blocktype == 2) || (blocktype == 3) || (blocktype == 4) || (blocktype == 5)) {                     [blockarraywalk addobject:testblock];                 } else {                     [blockarray addobject:testblock];                 }                   [background addchild:testblock];                 intx++;                 if (intx == 25) {                     intx = 0;                     inty++;                 }             }         }     }     sqlite3_close(blockminerdb); } 

[self buildmap] uses sqlite retreive map database, i'm looping while loop , causing crash. loops 20-25 times (of 600) before crashing.

within loop i'm creating new block (skspritenode subclass) places block @ position information (position, name, physics , such, no heavy stuff). using nsmutablearray store blocks easy access , thought problem @ first, when remove [blockarray addobject:block]; app still crashes.

if remove every bit of code in -(void)buildmap app doesn't crash.

is there dispatch_async , while loop might cause this?

i can't access code right if need see more code, can add in 8 hours ;) appreciate help, trying solve error 3 weeks on , off :d

at point in time sprite kit may enumerating on children (perhaps draw them) , if dispatch block adds new child node, cause children array mutate while being enumerated.

you fill array of to-be-added nodes in dispatch block, , after dispatch block add them @ once.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -