我是Objective C(IOS)開發人員中的新成員,我想知道爲什麼我的循環下的代碼正在執行,即使我的循環尚未完成。如何在IOS目標c中完成循環後運行一行代碼?
這裏是我的代碼:
-(void)update_tbl_selectItem_data{
db = [[DbHandler alloc] init];
ComputeProductPrice *computeProductPrice = [[ComputeProductPrice alloc]init];
[computeProductPrice ProductPriceComputation];
NSLog(@"count: %i", [g_allItemId count]);
for (int i = 0; i < [g_allItemId count]; i++) {
NSLog(@"itemID: %@", [g_allItemId objectAtIndex:i]);
NSString *l_uomqty = [[DbHandler database] getUomQty:[g_allItemId objectAtIndex:i]];
[db insertDataIn_tbl_selectItem_data:g_employeeid ProdId:[g_allItemId objectAtIndex:i] ProdName:[g_allDesc objectAtIndex:i] GenName:[g_allGenName objectAtIndex:i] ComputeType:[g_allType objectAtIndex:i] UOM:[g_allUOM objectAtIndex:i] ListPrice:[g_allPrice objectAtIndex:i] UOMQty:l_uomqty];
}
[db updateDatabaseHasChanges:g_employeeid];
[[NSNotificationCenter defaultCenter] postNotificationName:@"performSegueSoldToToSelectItem" object:nil];
}
我的循環下,本準則執行,即使我的循環沒有完成呢。
[db updateDatabaseHasChanges:g_employeeid];
[[NSNotificationCenter defaultCenter] postNotificationName:@"performSegueSoldToToSelectItem" object:nil];
我希望我的那一行代碼能夠在我的循環之後執行。請幫助我堅持這一點。
編輯
這裏是數據庫方法:
-(void)insertDataIn_tbl_selectItem_data: (NSString *)empID ProdId: (NSString *)prodId ProdName: (NSString *)prodName GenName: (NSString *)genName ComputeType: (NSString *)computeType UOM: (NSString *)uom ListPrice: (NSString *)listPrice UOMQty: (NSString *)uomqty{
// NSString *query = [NSString stringWithFormat:@"insert into tbl_selectItem_data (femployeeid,fproductid,fname,fgeneric_name,fcompute_type,fuom,flist_price,fuomqty) values ('%@','%@', '%@', '%@', '%@', '%@', '%@', '%@')", empID, prodId, prodName, genName, computeType, uom, listPrice, uomqty];
const char *query = "insert into tbl_selectItem_data (femployeeid,fproductid,fname,fgeneric_name,fcompute_type,fuom,flist_price,fuomqty) values (?,?, ?, ?, ?, ?, ?, ?)";
sqlite3_stmt *stmt;
//char *err;
if (sqlite3_open([l_SqliteDb UTF8String], &(_database)) == SQLITE_OK) {
if (sqlite3_prepare_v2(_database, query, -1, &stmt, nil) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [empID UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 2, [prodId UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 3, [prodName UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 4, [genName UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 5, [computeType UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 6, [uom UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 7, [listPrice UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 8, [uomqty UTF8String], -1, NULL);
if(sqlite3_step(stmt) == SQLITE_DONE){
NSLog(@"Insert Successful");
sqlite3_finalize(stmt);
sqlite3_close(_database);
}else{
NSLog(@"insertDataIn_tbl_selectItem_data error: %s", sqlite3_errmsg(_database));
}
sqlite3_finalize(stmt);
}
}
sqlite3_close(_database);
}
-(void)updateDatabaseHasChanges: (NSString *)empId{
NSString *query = [NSString stringWithFormat:@"UPDATE tbl_employee_has_changes SET has_changes=0 WHERE femployeeid='%@'", empId];
sqlite3_stmt *l_statement;
if (sqlite3_open([l_SqliteDb UTF8String], &(_database)) == SQLITE_OK) {
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &l_statement, nil) == SQLITE_OK) {
if (sqlite3_step(l_statement) == SQLITE_DONE) {
NSLog(@"Update Successful");
sqlite3_finalize(l_statement);
sqlite3_close(_database);
}else{
NSLog(@"updateDatabaseHasChanges error: %s", sqlite3_errmsg(_database));
sqlite3_finalize(l_statement);
sqlite3_close(_database);
}
}
}
sqlite3_close(_database);
}
它不會發生。嘗試在for循環後放入nslog,並檢查它是否記錄在for循環日誌的末尾或中間。當您調用insertData方法時,可能會調用updateDatabse方法。 – santhu
我很高興地看到,我的答案(來自你的其他問題)幫助你插入到SQL中......但是,如果你甚至沒有回答,沒有人會在未來幫助你:( – TonyMkenu
@TonyMkenu讓我們來我的問題很簡單,我有我的方法(1),它內部運行一個循環,然後我有另一個方法(2),它將調用頂部的方法(1),並在方法(1)的調用下有一個NSlog, 。但我的問題是我想在完成循環(方法1)之後完成循環(Method1)方法(2) – NewDroidDev