簡體   English   中英

核心數據:- 如何更新數組中的值(可轉換)

[英]Core data:- How to update values in the array (Transformable)

我正在開發使用核心數據將數據保存在本地設備中的應用程序。 在核心數據中,我使用可轉換格式將數據保存在數組中,但是我不知道如何更新數組中的特定值。 我的更新數組代碼在這里

 NSManagedObjectContext *context = [self managedObjectContext];
   NSManagedObject *user = [NSEntityDescription    insertNewObjectForEntityForName:@"Type" inManagedObjectContext:context];
 NSError *error = nil;
 //Set up to get the thing you want to update
 NSFetchRequest * request = [[NSFetchRequest alloc] init];
   NSEntityDescription *entity = [NSEntityDescription entityForName:@"Type"inManagedObjectContext:context];
 [request setEntity:entity];
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"businessTypes == %@", @"Others"];
 [request setPredicate:predicate];

   AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication]delegate];
  NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];

if (results == nil) {
    // This implies an error has occurred.
    NSLog(@"Error from Core Data: %@", error);
} else {
    if (results.count == 0) {
        // No objects saved, create a new one...
    } else {
        // At least one object saved.  There should be only one
        // so use the first...
        user = [results lastObject];
        [user setValue:@"Management" forKey:@"businessTypes"];
    }
}

if (![self.managedObjectContext save:&error]) {
   //Handle any error with the saving of the context
}
else{
  [app saveContext];
   NSLog(@"update value successfully");
}

下面是我在核心數據中的保存數組:

{
    businessTypes =         (
        "Social Bussiness",
        Marketing,
        Transports,
        Others
    );
},

所以我想將數組中的“其他”更新為“管理”。

當我運行此代碼時,我沒有錯誤,但我不會更新索引數組中的特定值。 感謝幫助我。

也許你正在混淆你的實體。 您獲取了一個名為Type的實體,但您正在調用對象user ,這表明您可能想要獲取具有特定業務類型的用戶。

如果每個用戶只有一個“業務類型”,則不需要Type實體,只需要User實體的字符串屬性。

如果每個用戶可以擁有多個業務類型,您應該有一個實體Type ,其name屬性包含一個指示業務類型的術語,並且應該建模為多對多關系。

User <<--------->>  Type

要將現在稱為“其他”的所有類型設置為“管理”,您需要獲取name “其他”的Type ,更改並保存。 要將用戶的業務類型之一從“其他”更改為“管理”,您需要:獲取用戶,刪除“其他”類型,獲取“管理”類型,將其添加到用戶並保存。

如果您的businessTypes屬性應該是帶有硬編碼字符串的可轉換數組,您可能應該如上所述更改數據模型。 使用干凈的 Core Data 模型搜索和處理數據時,您將擁有更大的靈活性和能力。

您必須像此代碼一樣修改更新功能,然后才能獲得所需的輸出

NSManagedObjectContext *context = [self managedObjectContext];
NSError *error = nil;
NSFetchRequest * request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Type"inManagedObjectContext:context];
[request setEntity:entity];
 request.propertiesToFetch= @[ @"businessTypes"];
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];

if (results == nil) {
    // This implies an error has occurred.
    NSLog(@"Error from Core Data: %@", error);
} else {
    if (results.count == 0) {
        // No objects saved, create a new one...
    } else {


        int loopCount = (int)results.count;
        Type* entityType = nil;
        for (int index=0; index<loopCount; index++) {
            entityType = (Type*)results[index];
            if (entityType.businessTypes!=nil) {
                NSUInteger reqIndex = [entityType.businessTypes indexOfObject:@"Others"];
                [entityType.businessTypes replaceObjectAtIndex:reqIndex withObject:@"Management"];
                [entityType setValue:entityType.businessTypes forKey:@"businessTypes"];
            }

        }
}
if (![self.managedObjectContext save:&error]) {
    //Handle any error with the saving of the context
}
else{
    [app saveContext];
    NSLog(@"update value successfully");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM