2

我有一种在卡上添加印章的方法,从二维码阅读器到网络服务,我的问题是,

这是我的代码:

- (void)addStamp
{    
    NSString *url = [_URL stringByAppendingString:@"add-stamp"];
    NSString *post = [NSString stringWithFormat:@"userId=%@&code=%@", self.userId, self.code ];
    post = [post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSData *postData = [NSData dataWithBytes:[post UTF8String]
                                      length:[post lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL 
    URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:600];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
}

现在我想用新方法更新我的数据库 - >如果代码重复添加戳,如果没有创建添加新卡:

-(void)addStampInDB:(int)cardId
{
    NSManagedObjectContext* managedObjectContext = [(AppDelegate*) [[UIApplication 
    sharedApplication] delegate] managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Card"
                                          inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"remote_id = %d", cardId];
    [fetchRequest setPredicate:predicate];
    [managedObjectContext lock];

    NSError *error;
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest
    error:&error];
    [managedObjectContext unlock];

    Card *d = nil;
    if([fetchedObjects count] > 0 ){
        d = [fetchedObjects objectAtIndex:0];
        cardId++;

    } else{ 
        d = [NSEntityDescription insertNewObjectForEntityForName:@"Card" 
                                          inManagedObjectContext:managedObjectContext];
    }

    NSArray *test;
    for(NSDictionary *stamp in test)
    {   
        d.remote_id = [NSNumber numberWithInt:[[stamp objectForKey:@"id"] intValue]];
        d.stampNumber = [NSNumber numberWithInt:[[stamp objectForKey:@"stampNumber"] intValue]];
        d.createdAt = [NSDate dateWithTimeIntervalSince1970:[[stamp objectForKey:@"createdAt"] 
        intValue]];

        [managedObjectContext lock];
        NSError *error;
        if (![managedObjectContext save:&error])
        {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
            NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
            NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
            if(detailedErrors != nil && [detailedErrors count] > 0) {
                for(NSError* detailedError in detailedErrors) {
                    NSLog(@"  DetailedError: %@", [detailedError userInfo]);
                }
            } else {
                NSLog(@"  %@", [error userInfo]);
            }
        }
        [managedObjectContext unlock];
    }
}

我不知道我的方法是否正确以及如何测试我的方法?

4

1 回答 1

0

我不太确定您的问题,但我会尝试提供一些提示。

第一的

Card *d = nil;
if([fetchedObjects count] > 0 ){
    d = [fetchedObjects objectAtIndex:0];
    cardId++;

} else{ 
    d = [NSEntityDescription insertNewObjectForEntityForName:@"Card" 
                                      inManagedObjectContext:managedObjectContext];
}

在这里你找到了一张现有的卡片,你使用它,否则你创建一个新的。问:那怎么cardId办?它在哪里使用?

第二

for(NSDictionary *stamp in test)
{   
    d.remote_id = [NSNumber numberWithInt:[[stamp objectForKey:@"id"] intValue]];
    d.stampNumber = [NSNumber numberWithInt:[[stamp objectForKey:@"stampNumber"] intValue]];
    d.createdAt = [NSDate dateWithTimeIntervalSince1970:[[stamp objectForKey:@"createdAt"] 
    intValue]];

    // other code here
}

如果你循环,你每次都会test覆盖你的卡的价值。d这样对吗?

第三

for(NSDictionary *stamp in test)
{   
    // other code here
}

// save here

将保存移出 for 循环。这可以提高您的应用程序的性能。不必在循环中每次都保存。只在最后做。

希望有帮助。

PS如果你想知道别的让我知道。

于 2013-03-05T22:31:59.247 回答