从 CoreData 获取特定数量的随机行

发布于 2024-09-24 08:09:07 字数 887 浏览 3 评论 0原文

我使用下面的代码来使用与搜索条件匹配的 CoreData 来获取所有行的查询集:itemType = 1。 但我需要做的是从数据中获取特定数量的随机行。 例如,我不需要检索列名 dataType = 1 的所有 100 行数据,而是需要随机获取 dataType = 1 的 25 行。 我希望有一个相对无痛的解决方案。 任何帮助表示赞赏。 lq

NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setEntity:[NSEntityDescription entityForName:@"MyAppName" 
                    inManagedObjectContext:[self managedObjectContext]]];

NSError *error = nil;                                           
NSPredicate *predicate;
NSArray *fetchResults;
predicate = [NSPredicate predicateWithFormat:@"(itemType = %i)", 1];            
[request setPredicate:predicate];
fetchResults = [managedObjectContext executeFetchRequest:request error:&error];

if (!fetchResults) {
        // NSLog(@"no fetch results error %@", error);
}

self.mutableArrayName = [NSMutableArray arrayWithArray:fetchResults];
[request release];

I'm using the code below to Fetch a queried set of all rows using CoreData matching the search criteria: itemType = 1.
But what I need to do is to Fetch a specific number of Random rows from the data instead.
For example, instead of retrieving all 100 rows of data in which the column name dataType = 1, I need to get 25 rows randomly in which dataType = 1.
I'm hoping there is relatively painless solution.
Any help is appreciated.
lq

NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setEntity:[NSEntityDescription entityForName:@"MyAppName" 
                    inManagedObjectContext:[self managedObjectContext]]];

NSError *error = nil;                                           
NSPredicate *predicate;
NSArray *fetchResults;
predicate = [NSPredicate predicateWithFormat:@"(itemType = %i)", 1];            
[request setPredicate:predicate];
fetchResults = [managedObjectContext executeFetchRequest:request error:&error];

if (!fetchResults) {
        // NSLog(@"no fetch results error %@", error);
}

self.mutableArrayName = [NSMutableArray arrayWithArray:fetchResults];
[request release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2024-10-01 08:09:07

您实际上无法获取随机行。合理的随机化策略可能是获取与谓词匹配的所有对象,然后随机选择特定数量的对象。

无论如何,您可以使用 NSFetchRequest 的以下方法:

- (void)setFetchLimit:(NSUInteger)limit
- (void)setFetchOffset:(NSUInteger)limit

基本上,setFetchLimit 允许您定义要获取的行数(在您的情况下,您将限制设置为 25),而 setFetchOffset 定义了开始返回行的偏移量(有关详细信息,请参阅 fetchOffset 属性的文档)。

这不是一个随机过程,但您可以随机生成偏移量。然而,这里值得注意的是,根据偏移量,您可能会获取一些介于零和获取限制之间的对象。

You can not actually fetch random rows. A reasonable randomization strategy may be to fetch all of the objects matching your predicate, and then randomly select a specific number of objects.

Anyway you can use the following methods of NSFetchRequest:

- (void)setFetchLimit:(NSUInteger)limit
- (void)setFetchOffset:(NSUInteger)limit

Basically, setFetchLimit allows you to define how many rows you want to fetch (in your case you will set limit to 25), while setFetchOffset defines the offset at which rows will begin being returned (see the documentation of the fetchOffset property for details).

This is not a random process, but you may randomly generate the offset. However, it is worth noting here that, depending on the offset, you may then fetch a number of objects falling between zero and your fetch limit.

烟沫凡尘 2024-10-01 08:09:07

您也可以使用参考方法。当您按观看次数排序时。
我很久以前就发布过关于它的文章: http://www.alterplay.com/ios-dev-tips/2010/06/fetch-random-record-with-coredata.html
抱歉格式化。从 Blogger 切换到 Wordpress 后,它就损坏了。

You could also use reference approach. When you sort by view counts.
I posted long time ago about it: http://www.alterplay.com/ios-dev-tips/2010/06/fetch-random-record-with-coredata.html
Sorry for formatting. it's broken after switching from Blogger to Wordpress.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文