开发者

Query for every nth element in core data

开发者 https://www.devze.com 2023-03-25 09:16 出处:网络
How do I query for every nTh (or just e开发者_StackOverflowvery second) Element in Core Data? Here is what I would do in SQL:

How do I query for every nTh (or just e开发者_StackOverflowvery second) Element in Core Data?

Here is what I would do in SQL:

SELECT * FROM TABLE_NAME 
WHERE (ROWID, 0) IN (SELECT ROWID, MOD(ROWID, N) FROM TABLE_NAME);

Thx in advance.


You can't.

Core Data is not SQL. Entities are not tables. Objects are not rows. Attributes are not columns. Relationships are not joins. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.

Managed Objects do not exist as "elements" in a table but in an object graph which is a web of individual objects connected by the relationships defined in your data model. Even if you use the option of an SQL store, the actually physical position of the decomposed object's attributes in the table will have nothing to do with object's location in the object graph.

If you want every nth object, you would first do a fetch with a sort descriptor ordering the objects by whatever criteria you want at the moment and then you would take every nth element of the array returned by the fetch.


SELECT * FROM table_name WHERE mod(ROWID,2)=0

if ROWID is a field in the table. Note that rows in a table have no "sequence number", although you may construct a ordinal in the query result

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号