簡體   English   中英

用於 iPhone 核心數據中一對多關系的 addObject

[英]addObject for one/many-to-many relationships in core data for iPhone

我的 xcdatamodel 中有以下 2 個實體:

  1. Matrix 1.1 屬性名稱 1.2 關系 MatrixToProcess 目標是:Process 逆是:ProcessToMatrix 選中對多關系 刪除規則是級聯

  2. 過程 2.1 屬性名稱 2.2 關系 ProcessToMatrix 目標是:矩陣 逆是:MatrixToProcess 不檢查多對多關系 刪除規則為 Nullify

我可以成功添加一個新的矩陣,該矩陣已添加並正確顯示在 UITableView 中。

我可以成功添加一個新進程,但是,所有必要的信息都添加到數據庫中,但矩陣表中的 Z_PK 值除外。 即 iPhone 模擬器中的 sqlite 數據庫將創建新的進程名稱,但不會在 ZPROCESSTOMATRIX 列中輸入任何信息。 如果我手動插入關聯的矩陣名稱 Z_PK 值,一切正常。

我在掙扎。 不完全理解如何在 Process *newProcess = [NSEntityDescription insertNewObjectForEntityForName:@"Process" inManagedObjectContext:self.managedObjectContext]; 下添加 addObject。 這是我正在使用的代碼:

- (void)addProcess:(id)sender {
    ProcessAddViewController *addController = [[ProcessAddViewController alloc] initWithNibName:@"ProcessAddView" bundle:nil];
    addController.delegate = self;  
    Process *newProcess = [NSEntityDescription insertNewObjectForEntityForName:@"Process" inManagedObjectContext:self.managedObjectContext];
    addController.process = newProcess;
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
    [self presentModalViewController:navigationController animated:YES];    
    [navigationController release];
    [addController release];
}

- (void)processAddViewController:(ProcessAddViewController *)processAddViewController didAddProcess:(Process *)process {
    if (process) {        
        [self showProcess:process animated:NO];
    }

    [self dismissModalViewControllerAnimated:YES];
}


- (void)showProcess:(Process *)process animated:(BOOL)animated {
    RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];
    rootViewController.managedObjectContext = self.managedObjectContext;    
    if(entitySearchPredicate == nil)
    {
        NSMutableArray* mutableFetchResults = [CoreDataHelper getObjectsFromContext:self.entityName :@"displayOrder" :YES :managedObjectContext];       
        [self setEntityArray:mutableFetchResults];
        [mutableFetchResults release];
    }
    else
    {
        NSMutableArray* mutableFetchResults = [CoreDataHelper searchObjectsInContext:self.entityName :entitySearchPredicate :@"displayOrder" :YES :managedObjectContext];
        [self setEntityArray:mutableFetchResults];
        [mutableFetchResults release];
    }
    [rootViewController release];
}

任何幫助和/或方向將不勝感激。

您需要設置新流程和矩陣之間的關系。

查看iphone 核心數據插入新對象問題的回答

更新

假設您已經為 Process 和 Matrix 生成了類文件,並且 Process 與名為“processToMatrix”的 Matrix 有關系,則設置關系的代碼將是:

newProcess.processToMatrix = matrix;

其中“矩陣”是應與新 Process 對象關聯的 Matrix 對象。

暫無
暫無

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

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