iOS Magical Record无法保存

时间:2014-04-12 09:53:57

标签: ios core-data magicalrecord

所以我似乎无法在我的tableview中显示任何数据,但后来我注意到上下文没有保存,我不断收到此错误:

[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x166a70b0) NO CHANGES IN ** DEFAULT ** CONTEXT - NOT SAVING
2014-04-12 19:49:04.722 0DataCollector[3218:60b] Managedcontext working (
    TestSuburb
)



- (IBAction)backToSuburbsTableViewController:(UIStoryboardSegue *)segue {

    if ([segue.sourceViewController isKindOfClass:[NewSuburbTableViewController class]]) {
        NSLog(@"Coming back from NewSuburbTableViewController");
        NewSuburbTableViewController *srcVC = segue.sourceViewController;
        suburbString = srcVC.suburbTextField.text;
        [suburbsArray addObject:suburbString];

        //Save Managed Object Context
        [[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"You successfully saved your context.");
            } else if (error) {
                NSLog(@"Error saving context: %@", error.description);
            }
        }];        NSLog(@"Managedcontext working %@", suburbsArray);


    }

我们非常感谢您提供的任何帮助,我也尝试过保存" contextWithinCurrentThread"没有帮助。

TableViewController的其余代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    suburbsArray = [[NSMutableArray alloc] init];

    [self fetchSuburbs];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)fetchSuburbs {
    //Fetch suburbs
    self.suburbsArray = [NSMutableArray arrayWithArray:[Suburb findAllSortedBy:@"name" ascending:YES]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self fetchSuburbs];

    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [suburbsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSMutableArray *reversedSuburbsArray = [[[suburbsArray reverseObjectEnumerator] allObjects] mutableCopy];

    //Fetch suburb
    Suburb *suburb = [self.suburbsArray objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = suburb.name;

    return cell;
}

1 个答案:

答案 0 :(得分:3)

也许你不是在创建和插入新的NSmanagedObject"郊区"进入上下文,所以没有变化。在这种情况下,您的问题不在于保存,没有什么可以保存。

添加到可变数组不会创建新的郊区。您只创建了一个与任何NSmanagedObject无关的字符串对象。

然后我建议:

- (IBAction)backToSuburbsTableViewController:(UIStoryboardSegue *)segue {

    if ([segue.sourceViewController isKindOfClass:[NewSuburbTableViewController class]]) {
        NSLog(@"Coming back from NewSuburbTableViewController");
        NewSuburbTableViewController *srcVC = segue.sourceViewController;
        suburbString = srcVC.suburbTextField.text;
        [suburbsArray addObject:suburbString];

        // Create the new suburb
        Suburb *theNewBurb = [Suburb createEntity];
        theNewBurb.burbName = suburbString; // I don't know what properties you have on Suburb so you would need to correct this
        // You also need to set any other required properties of Suburb prior to save or it will fail

        //Save Managed Object Context
        [[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"You successfully saved your context.");
            } else if (error) {
                NSLog(@"Error saving context: %@", error.description);
            }
        }];        NSLog(@"Managedcontext working %@", suburbsArray);


    }