2

QRコードリーダーからWebサービスまで、カードにスタンプを追加する方法があります。私の質問は、

これが私のコードです:

- (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];
}

現在、DB を新しいメソッドで更新したい -> コードが繰り返される場合はスタンプを追加し、作成されない場合は新しいカードを追加します。

-(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