Web サービスからデータをフェッチし、コア データに解析するアプリがあります。
アプリはタブバー ベースで、最初のタブIntroViewController
は環境設定に使用されます。ユーザーがストアをフィルタリングできるオプションは 2 つあります。
ドライブ スルーDriveThru
は、が設定されているセルをタップするとアクティブになりますself.driveThru = TRUE
。
[今すぐ開く]は、 をタップして有効にしOpen Now Cell
ますself.open = TRUE
。
場所のすべてのデータはデータベースに保存されます。の下部にはIntroViewController
、タブに移動するための [検索] ボタンがありtableviewcontroller
ます。これ:
self.stores
コア データ フェッチから配列を生成します- 開いた文字列または閉じた文字列に設定されたプロパティを使用して
MyLocation
オブジェクトを作成しますestado
- その配列をソートし、その配列をソート済みの配列として
MyLocation
再設定しますself.annotationsToSort
- 最後に、ソートされたバージョンを使用して
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];
}
これにより、すべての店舗が返されます。したがって、配列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
。そのためには、どのようにデータを操作すればよいですか? 私が思いついた唯一のことは次のとおりです。
self.annotationsToSort
ソート前に循環- オブジェクトなしで新しい配列を作成します。
estado = closed
- 次に並べ替え
助言がありますか?