CoreData删除后保存数据

时间:2014-03-31 11:59:54

标签: ios objective-c cocoa core-data nsnumber

我使用带有2个实体的核心数据一个是我的存档,有图像参考,名称和NSNumber第二个实体是mi数字余额。

在档案视图中,我可以将档案实体加载到UITable视图,如果我点击我的单元格,我有完美的细节,我的按钮可以设置卡片。

在我删除按钮的详细信息中,这具有删除单张卡的功能,并且在余额实体中更改数值。

如果我尝试删除工作正常并且Balance加载了新值,但是我添加新卡,我的余额添加新值加上最后删除的值:

启动应用程序添加我的名片,图像和值(10),余额显示1张卡,总共10张,即可。

转到我的存档选择我的卡和删除,现在我有空档案,我的余额显示0卡和0值,现在如果我添加其他卡片值10,我的余额播种2卡和20值。

这是用于删除的代码:

- (IBAction)delete:(id)sender {

    NSError *error;

    NSManagedObjectContext *context = [self managedObjectContext];

    [context deleteObject:loadA[_currentRecord]];

//image remover from documents folder//
    NSString *imageRemover = [[self docsDir]stringByAppendingPathComponent:nomeSc.text];
    [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@.jpg", imageRemover] error:&error];

//----------------------------------//
    if (![context save:&error]){

        NSLog(@"Save Error, please retry: %@", [error localizedDescription]);
    }

    [self refreshBalance];

    _currentRecord =-1;

}

- (void)refreshBalance:(id)sender {

    NSManagedObjectContext *context = [self managedObjectContext];

    if (_currentRecord <= -1) {

        modifyA = loadA[_currentRecord];
        modifyB = loadB[currentBalance];

    }

        int oldBet = [betOld.text intValue];
        int cardBetting = [modifyB.totalCards intValue];

        int amauntBet = [blDetail[0] intValue];



        modifyB.totBet = [NSNumber numberWithInteger:amauntBet - oldBet];
        modifyB.totCards = [NSNumber numberWithInt:cardBetting -1];
    }


    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Errore durante il salvataggio: %@", [error localizedDescription]);
    }

    _currentRecord = -1;
    _currentBalance = -1;

    [self.navigationController popViewControllerAnimated:YES];

}

** * ** * **** 这是保存的功能 * ** * ** * ** * ** * ** * *

- (IBAction)onSave:(id)sender{

    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *imageName = self.name.text;
    NSString* foofile = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", imageName]];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];

    if (fileExists == YES){

        NSString *titolo = @"Warning";
        NSString *messaggio = [NSString stringWithFormat:@"Your name ( %@ ) exist, please changeit!", imageName];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];

    } else


    if (nomeGiocata.text.length == 0 || importoGiocato.text.length == 0 ) {

        NSString *titolo = @"Warning";
        NSString *messaggio = @"There are empty fileds!";

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];

    } else {

        NSString *imageName = self.nomeGiocata.text;
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        UIImage * imageToSave = self.imageViewPick.image;
        NSData * binaryImageData = UIImageJPEGRepresentation(imageToSave, 1.0);
        [binaryImageData writeToFile:[basePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", imageName]] atomically:YES];

        NSManagedObjectContext *context = [self managedObjectContext];
        [self.managedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];


        newScheda = [NSEntityDescription
                    insertNewObjectForEntityForName:@"Archive"
                    inManagedObjectContext:context];

        newScheda.immagine = [NSString stringWithFormat:@"%@.jpg", imageName];
        newScheda.nome = nomeGiocata.text;

        int bet = [importoGiocato.text intValue];
        newScheda.giocata = [NSNumber numberWithInt:giocata];

        addSched = balance[currentRecord];

        int insert = [addSched.totScommesso intValue];
        addSched.totScommesso = [NSNumber numberWithInt:insert + bet];

        int aggiungiSched = [addSched.totSchedGiocate intValue];
        addSched.totSchedGiocate = [NSNumber numberWithInt:aggiungiSched + 1];

        int balance = [addSched.totBilancio intValue];
        addSched.totBilancio = [NSNumber numberWithInt:balance + giocata];


        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Error during the process: %@", [error localizedDescription]);
        }

        NSString *titolo = @“MyApp";
        NSString *messaggio = @"Saved!";

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }

    [self clearFields];

}

删除记录时如何清空缓存?还是有其他想法?

0 个答案:

没有答案