0

「パス」または実行ごとに複数の場所を保存する GPS 実行アプリを作成しています。ORM スタイルの DB の新しい概念を理解するのに少し苦労しています。(Core Data は SQLLite を基礎となるメカニズムとして使用し、真の ORM ではないことは知っていますが、それはさておき..) 「To-Many」関係から 2 つのエンティティ「パス」と「場所」を設定しています。カスケード削除を使用したパス>>ロケーション、および削除を無効にするだけの逆です。

ここに画像の説明を入力 ここに画像の説明を入力

私がやっている:

//SEND TO CORE DATA
        bdAppDelegate *appDelegate =
        [[UIApplication sharedApplication] delegate];

        NSManagedObjectContext *context =
        [appDelegate managedObjectContext];

        // get GPS lat/lng
        double lat = newLocation.coordinate.latitude;
        double lng = newLocation.coordinate.longitude;

        // insert Path
        Path *currentPathInfo = [NSEntityDescription insertNewObjectForEntityForName:@"Path" inManagedObjectContext:context];
        currentPathInfo.name = currentPathName;
        currentPathInfo.createddate =[NSDate date];

        // insert location/GPS point
        Location *currentPathLocation = [NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:context];
        currentPathLocation.lat = [NSNumber numberWithDouble:lat];
        currentPathLocation.lng = [NSNumber numberWithDouble:lng];

        // insert 'Location' relationship to 'Path'
        [currentPathInfo addLocationsObject:currentPathLocation];

        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }

これは最初の起動時 (ボタンを押す) ですが、上記のコードを繰り返した結果として「パス」を追加し続けることなく、最初の「パス」に関連する「場所」を追加し続けるにはどうすればよいですか?

SQLでは、場所を挿入するために「場所」テーブルの外部キーに使用するPathID整数(ID)があります...切断されています...

4

2 に答える 2

0

私が理解していなかったのはこれでした: FetchRequest を実行して、パスが存在するかどうかを確認する必要があります。

- (NSArray *)checkIfPathExistsByName {
// check first if this path exists ..
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"Path" inManagedObjectContext:context];
[request setEntity:entityDescription];

// Set WHERE clause
NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"(name LIKE %@)", currentPathName];
[request setPredicate:predicate];

// sort
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:NO];
[request setSortDescriptors:@[sortDescriptor]];

NSError *error;
NSArray *array = [context executeFetchRequest:request error:&error];
return array;

これを locationManager デリゲートで呼び出します

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

// check first if this path exists ..
NSArray *array;
array = [self checkIfPathExistsByName];

if (array == nil)
{
    // Deal with error...
}
else
{
    if (array.count == 0){
        // if exists, just add Locations
        [self startNewPathAndInsertFirstCoordinate:newLocation forPathName:currentPathName];
    }else{            
        // start new if doesn't exist
        Path *myPath = [array lastObject];
        [self insertNewCoordinate:newLocation forPath:myPath];
    }
}

ここで、新しいパスの場合は currentPathName と場所を startNewPathAndInsertFirstCoordinate に渡しますが、パス (array.count>0) が見つかった場合は、配列からパスを取り出して InsertNewCoordinate という別のメソッドに渡します。パスを取得したため、insertNewCoordinate では、次のように挿入時に位置とパスの関係を設定できます。

- (void)insertNewCoordinate:(CLLocation *)newLocation forPath:(Path *)currentPath{

//SEND TO CORE DATA    
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSLog(@"howRecent: %f\n",howRecent);    

// get GPS lat/lng
double lat = newLocation.coordinate.latitude;
double lng = newLocation.coordinate.longitude;

// insert location/GPS point
Location *currentPathLocation = [NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:context];
currentPathLocation.lat = [NSNumber numberWithDouble:lat];
currentPathLocation.lng = [NSNumber numberWithDouble:lng];

// insert 'Location' relationship to 'Path'
[currentPath addLocationObject:currentPathLocation];

NSError *error;
if (![context save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}}

- (void)startNewPathAndInsertFirstCoordinate:(CLLocation *)newLocation forPathName:(NSString *)pathName{

NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSLog(@"howRecent: %f\n",howRecent);

if (abs(howRecent) < 35.0)
{
    //Location timestamp is within the last 15.0 seconds, let's use it!
    NSLog(@"horizontalAccuracy: %f\n",newLocation.horizontalAccuracy);
    if(newLocation.horizontalAccuracy < 85.0){
        //Location seems pretty accurate, let's use it!
        NSLog(@"latitude %+.6f, longitude %+.6f\n",
              newLocation.coordinate.latitude,
              newLocation.coordinate.longitude);
        NSLog(@"Horizontal Accuracy:%f", newLocation.horizontalAccuracy);

        //SEND TO CORE DATA

        // get GPS lat/lng
        double lat = newLocation.coordinate.latitude;
        double lng = newLocation.coordinate.longitude;

        // insert Path
        Path *currentPathInfo = [NSEntityDescription insertNewObjectForEntityForName:@"Path" inManagedObjectContext:context];
        currentPathInfo.name = currentPathName;
        currentPathInfo.createdDate =[NSDate date];

        // insert location/GPS point
        Location *currentPathLocation = [NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:context];
        currentPathLocation.lat = [NSNumber numberWithDouble:lat];
        currentPathLocation.lng = [NSNumber numberWithDouble:lng];

        // insert 'Location' relationship to 'Path'
        [currentPathInfo addLocationObject:currentPathLocation];

        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
    }
}
[self.locationManager startUpdatingLocation];

}

于 2013-10-02T04:40:21.963 に答える