2013-05-09 49 views
5

我有一個表用戶和核心數據模型中有一堆列。其中一列是user_id,我試圖獲得最高的user_id並向其中添加+1,並將其傳遞給將要插入的下一個對象。我遵循apple developer guide並按照建議執行。這是我試圖執行的代碼。從核心數據和sqlite獲取最大ID

問題是:我正在返回的account_id值被設置爲一些奇怪的數字,甚至在數據庫中不存在。

「account_id」= 139784529;它應該是1,因爲我只保存了核心數據。

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Users" inManagedObjectContext:self._managedObjectContext]; 
[request setEntity:entity]; 

// Specify that the request should return dictionaries. 
[request setResultType:NSDictionaryResultType]; 

// Create an expression for the key path. 
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"account_id"]; 

// Create an expression to represent the function you want to apply 
NSExpression *expression = [NSExpression expressionForFunction:@"max:" 
                arguments:@[keyPathExpression]]; 

// Create an expression description using the minExpression and returning a date. 
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 

// The name is the key that will be used in the dictionary for the return value. 
[expressionDescription setName:@"maxAccountId"]; 
[expressionDescription setExpression:expression]; 
[expressionDescription setExpressionResultType:NSInteger32AttributeType]; // For example, NSDateAttributeType 

// Set the request's properties to fetch just the property represented by the expressions. 
[request setPropertiesToFetch:@[expressionDescription]]; 

// Execute the fetch. 
NSError *error; 
NSArray *objects = [self._managedObjectContext executeFetchRequest:request error:&error]; 
if (objects == nil) { 
    // Handle the error. 
} 
else { 
    if ([objects count] > 0) { 
     nextAccountId = [[[objects objectAtIndex:0] valueForKey:@"maxAccountId"] integerValue]; 
    } 
} 
} 

@catch (NSException *exception) { 
    NSLog(@"%@",[exception reason]); 
} 

return nextAccountId + 1; 

希望有人遇到過這個問題,並修復它。

謝謝

+0

您看到的值看起來像您從訪問未初始化的變量中獲得的「正常」垃圾值。將'nextAccountId'設置爲零應該可以解決這個問題。但我不確定爲什麼'[objects count]'返回零。 – dasblinkenlight 2013-05-09 01:50:56

+0

[objects count]不返回零。我不確定你指的是什麼? – Harish 2013-05-09 10:26:32

+0

在某些路徑中看起來不像是'nextAccountId'的賦值,特別是當count爲零時。 Daniel的回答在下面加了一個'else'來說明這一點。 – dasblinkenlight 2013-05-09 10:29:35

回答

3

謝謝大家看看這個問題。也感謝您的評論和答覆。搞清楚從終端上查看sqlite數據庫的問題後,我注意到該表已損壞,並將所有垃圾數據返回給我。這個問題非常簡單。這是我爲解決它而做的。

  1. 從iPhone模擬器中刪除應用程序。
  2. 乾淨的解決方案
  3. 重新編譯程序。冉它,而巴姆..它的工作。

只是一個開發尖端對於那些誰不知道從終端約訪問源碼。

  1. 找出你的sqlite的位置。通常將 /User/UserName/Library/Application Support/iPhone Simulator/6.1/xxxxxxxxxx/Documents cd cd到此目錄。

  2. 型sqlite3的命令

  3. 附加 「Application.sqlite」 爲DB1

  4. 咣..運行你的命令。

  5. 如果你想看看內運行的數據庫 - 型.database

再次謝謝大家。

1

之後我刪除了try/catch語句代碼,簡化了if語句抓取後要求此此代碼工作正常,我:

if ([objects count] > 0) { 
    nextAccountId = [[[objects objectAtIndex:0] valueForKey:@"maxAccountId"] integerValue]; 
} 
else { 
    nextAccountId = 0; 
} 

如果仍不能解決問題確認您擁有正確的初始化管理對象上下文(self._managedObjectContextself.managedObjectContext,具體取決於您如何定義它),並且實體/屬性名稱是正確的。

+0

謝謝你的迴應,我會試試這個,讓你知道它是如何工作的。 – Harish 2013-05-09 10:28:14

+0

我試過你說的話,它仍然給我同樣的問題。安裝日誌,在註釋 (LLDB)PO對象 $ 12 = 0x08156790 <_PFArray 0x8156790>( { maxAccountId = 139784529; } ) – Harish 2013-05-09 19:02:13