0

Web サービスからデータをフェッチし、コア データに解析するアプリがあります。

タブバーのデザイン

アプリはタブバー ベースで、最初のタブIntroViewControllerは環境設定に使用されます。ユーザーがストアをフィルタリングできるオプションは 2 つあります。

ドライブ スルーDriveThruは、が設定されているセルをタップするとアクティブになりますself.driveThru = TRUE

[今すぐ開く]は、 をタップして有効にしOpen Now Cellますself.open = TRUE

IntroViewController

場所のすべてのデータはデータベースに保存されます。の下部にはIntroViewController、タブに移動するための [検索] ボタンがありtableviewcontrollerます。これ:

  1. self.storesコア データ フェッチから配列を生成します
  2. 開いた文字列または閉じた文字列に設定されたプロパティを使用してMyLocationオブジェクトを作成しますestado
  3. その配列をソートし、その配列をソート済みの配列としてMyLocation再設定しますself.annotationsToSort
  4. 最後に、ソートされたバージョンを使用してtableview

ユーザーが introviewcontroller でドライブスルー オプションをオンにした場合、テーブルビュー タブにはドライブスルーのあるものだけが表示されます。これはうまくいきます。

ここで、introviewcontroller の [今すぐ開く] オプションをオンにすると、開いている店舗のみが表示されます。

データベースから取得する現在の tableviewcontroller メソッドは次のとおりです。

- (void)loadRecordsFromCoreData {
    [self.managedObjectContext performBlockAndWait:^{
        [self.managedObjectContext reset];
        NSError *error = nil;
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:self.entityName];
        [request setSortDescriptors:[NSArray arrayWithObject:
                                     [NSSortDescriptor sortDescriptorWithKey:@"nombrePublico" ascending:YES]]];
        //Add Predicate
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(driveThru == %@)", [NSNumber numberWithBool:self.driveThru]];
        [request setPredicate:predicate];
        self.stores = [self.managedObjectContext executeFetchRequest:request error:&error]; 
    }];
    [self populateLocationsToSort];
}

場所を含む self.stores 配列

これにより、すべての店舗が返されます。したがって、配列populateLocationsToSortを循環するときにメソッドでこれを行いました。self.stores

for (Location * locationObject in self.stores) {
        // 3. Unload objects values into locals
        NSString * storeDescription = locationObject.nombrePublico;
        NSString * address = locationObject.direccion;
        NSString * hor_LV = locationObject.hor_LV;
        NSString * hrs24 = locationObject.hrs24;
        NSString * driveThru = locationObject.driveThru;
        ...
        NSString * estado;

        // Set it based on TimeComparator
        if ([TimeComparator dealWithTimeStrings2:locationObject.hor_LV]) {
            estado = @"Open";
        } else {
            estado = @"Closed";
        }

        // 4. Create MyLocation object based on locals gotten from Custom Object
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = latitude.doubleValue;
        coordinate.longitude = longitude.doubleValue;
        MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0 hrs24:hrs24 driveThru:driveThru hor_LV:hor_LV  estado:estado];

        // 5. Calculate distance between locations & uL
        CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
        CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:self.userLocation];
        annotation.distance = calculatedDistance/1000;

        //Add annotation to local NSMArray
        [self.annotationsToSort addObject:annotation];

    }
[self sort];
...

これで、self.annotationsToSort配列にはデータベースの Location オブジェクトが含まれるようになりました。これらのオブジェクトestadoには、現在の時間に基づいてオープンまたはクローズのフィールドが含まれるようになりました。

しかし、ここで、.open ストアのみを表示する必要がありますtableviewcontroller。そのためには、どのようにデータを操作すればよいですか? 私が思いついた唯一のことは次のとおりです。

  1. self.annotationsToSortソート前に循環
  2. オブジェクトなしで新しい配列を作成します。estado = closed
  3. 次に並べ替え

助言がありますか?

4

0 に答える 0