gpt4 book ai didi

ios - CoreData 按关系排序

转载 作者:行者123 更新时间:2023-11-28 12:47:52 34 4
gpt4 key购买 nike

我的关系如下图所示。

CoreData relation

我想先按事件中的事件对事件进行排序,然后再按开始日期对事件进行排序。如果当前日期介于开始日期和结束日期之间,则促销有效。不幸的是,由于 CoreData,我无法使用 transient 属性进行排序。在我的 Controller 中,我没有使用获取 Controller 。

有什么办法可以实现吗?

更新:

我有以下排序描述符:

// First is incorrect
[NSSortDescriptor(key: "promotion.start", ascending: false),
NSSortDescriptor(key: "start", ascending: true)]

谓词(虽然它们没问题):

let promotionsPredicate =
NSPredicate(format: "(%@ >= promotion.start && %@ <= promotion.end) && " +
"(ANY promotion.cities.id == %@)", NSDate(), NSDate(), objectID)
let eventsPredicate =
NSPredicate(format: "start >= %@ && venue.city.id == %@",
NSDate(), objectID)

let subpredicates = [eventsPredicate, promotionsPredicate]
let compoundPredicate NSCompoundPredicate(orPredicateWithSubpredicates: subpredicates)

这是请求(我使用的是 CoreStore,但思路应该很清楚):

class func pagedEventsForPredicateSortedByInPromoAndStartDate(predicate: NSPredicate,
descriptors: [NSSortDescriptor],
fetchOffset: Int,
fetchLimit: Int) -> [Event] {

return CoreStore.fetchAll(From(Event),
Where(predicate),
OrderBy(descriptors),
Tweak { (fetchRequest) -> Void in
fetchRequest.fetchOffset = fetchOffset
fetchRequest.fetchLimit = fetchLimit
fetchRequest.returnsObjectsAsFaults = false
}) ?? []
}

最佳答案

据我了解,您必须获取所有 Event 对象,但顺序要正确。要用如此复杂的顺序来做到这一点,其中包括关系,据我所知,您必须获取所有事件,然后使用 NSArray 的方法

对它们进行排序
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator)cmptr

代码如下

<强>1。从核心数据中获取

// get the right context here
NSManagedObjectContext *yourContext;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Event"];
// extra line, predicate is nil by default, any other required predicate could be written here
request.predicate = nil;

__block NSArray *results = nil;
[yourContext performBlockAndWait:^{

NSError *error = nil;
results = [yourContext executeFetchRequest:request error:&error];

if (error) {
// handle error here
}
}];

Fetch 是通过核心方法手动完成的,您可以使用 Magical Record 或任何其他与 Core Data 配合使用的框架来连续完成。

<强>2。对结果进行排序

__weak typeof(self) weakSelf = self;

NSDate *now = [NSDate date];
NSArray *sortedResults = [results sortedArrayUsingComparator:^NSComparisonResult(Event *_Nonnull obj1, Event *_Nonnull obj2) {

BOOL isObj1InActivePromotion = [weakSelf date:now isBetweenDate:obj1.promotion.start andDate:obj1.promotion.end];
BOOL isObj2InActivePromotion = [weakSelf date:now isBetweenDate:obj2.promotion.start andDate:obj2.promotion.end];

// if they eather are in active promotion or no, just compare them by start date of the Event
if (isObj1InActivePromotion == isObj2InActivePromotion) {
return [obj1.start compare:obj2.start];
} else {
return isObj1InActivePromotion ? NSOrderedAscending : NSOrderedDescending;
}
}];

<强>3。使用 NSDate 的其他方法

在排序方法中使用了该方法

+ (BOOL)date:(NSDate *)date isBetweenDate:(NSDate *)beginDate andDate:(NSDate *)endDate
{
if ([date compare:beginDate] == NSOrderedAscending) {
return NO;
}

if ([date compare:endDate] == NSOrderedDescending) {
return NO;
}

return YES;
}

由于显而易见的原因,我无法检查代码,如果有错别字,我们深表歉意。

关于ios - CoreData 按关系排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37499548/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com