Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Core Data : fetchBatchSize vs fetchOffset

I'm new to ios swift programming. I have a large working set of data in my application (Core data) and i want to restrict it. I came across two options fetchBatchSize and fetchOffset.

In official documentation both offers same use

fetchBatchSize You can use this feature to restrict the working set of data in your application. In combination with fetchLimit, you can create a subrange of an arbitrary result set.

fetchOffset This property can be used to restrict the working set of data. In combination with fetchLimit, you can create a subrange of an arbitrary result set.

Whats the differences between two?

like image 936
Arunkrishna Avatar asked Sep 15 '25 07:09

Arunkrishna


1 Answers

Lets say you have following objects in Core Data - A, B, C, D, E, F, G

If you try to fetch all objects you get [A, B, C, D, E, F, G]

If you set an fetchOffset of 2 you will get [C, D, E, F, G], so it will fetch everything after the specified fetchOffset.

fetchBatchSize is used so that Core Data does not fetch all objects at once, lets say in this example you set fetchBatchSize to 2, it would fetch first two objects, so A and B and rest would be faults, when you would try to access C core data would fulfil the fault and get C and D as well.

Lets say you have 1000 names, and you list them in a UITableView you can only show 20 names at a time, it makes sense to set fetchBatchSize to a bit higher value than screenful of names, so if user never scrolls Core Data only fetched first 30 objects for example, as soon as user scrolls down Core Data will fetch in next 30 and next 30 objets...

like image 66
Ladislav Avatar answered Sep 17 '25 23:09

Ladislav