prefetchRowsAt not called for last rows - ios

I am facing one issue in the UITableViewDataSourcePrefetching implementation.
I have 62 records total in the cloud and I am calling the API which returns 15 records per page.
I have implemented the prefetchRowsAt method as well which partially works fine for me.
The issue is the above method does not return all the indexPath it is going to load so as you can see from below logs it goes up to 53 only and in my table view it shows 60 records only.
Indexpaths to fetch : [[3, 51], [3, 42], [3, 52], [3, 41], [3, 53], [3, 40], [3, 54], [3, 39], [3, 55], [3, 38], [3, 56], [3, 37], [3, 57], [3, 36], [3, 58], [3, 35], [3, 59], [3, 34], [3, 60], [3, 33]]
Indexpaths to fetch : [[3, 51], [3, 50], [3, 49], [3, 48], [3, 47], [3, 46], [3, 45], [3, 44], [3, 43]]
Indexpaths to fetch : [[3, 52]]
Indexpaths to fetch : [[3, 53]]
Here is my method implementation
extension MyViewController:UITableViewDataSourcePrefetching{
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
#if DEBUG
print("Indexpaths to fetch : \(indexPaths)")
#endif
if indexPaths.contains(where: isLoadingCell) {
self.loadFurthersSessionNotification?()
}
}
}
private extension MyViewController {
func isLoadingCell(for indexPath: IndexPath) -> Bool {
if indexPath.section != 3{
return false
}
return (indexPath.row - 1) >= self.aryCloudObjects.count
}
}
Any help will be appreciated.
Thanks

There is nothing unexpected with the behaviour you are seeing. There is no guarantee that prefetchRowsAt will be called for every row in your table.
From the documentation:
Table views do not call this method for cells they require immediately, so your data source object must also be able to fetch the data itself.
Prefetching allows you to be prepared to display rows, but if the data hasn't been prefetched when cellForRowAt: is called then you need to fetch the data then.

Related

grails 3.3 gorm where query with projection count() different than list().size()

According to the Gorm 6 documentation section 7.4 the Where Query returns a DetachedCriteria, which provides a method count() that is supposed to return the number of records returned by the query. In fact, as far as I can tell, if dc is an instance of DetachedCriteria, then
dc.count() == dc.list().size()
must hold true.
In the case of a Where Query containing a projection, count() seems to return something else. In this simple example:
query = Runner.where {
projections {
groupProperty 'finishPosition'
rowCount()
}
}
println "query.count() ${query.count()}"
println "query.list().size() ${query.list().size()}"
the result printed is:
query.count() 576
query.list().size() 22
If I also print query.list(), it appears as
query.list() [[14, 576], [12, 1945], [8, 5682], [17, 78], [1, 91842], [15, 174], [10, 3836], [11, 2873], [4, 90688], [18, 36], [0, 336177], [16, 110], [6, 63957], [19, 6], [2, 91669], [21, 2], [3, 91550], [20, 4], [13, 956], [5, 72852], [9, 4811], [7, 6238]]
that is, list() and list.size() are consistent (and match an SQL query on the underlying database).
Does anyone have any ideas about why count() seems to off in this case? I find it interesting that the number returned by count() - 576 - is the same as the projection rowCount() for the first record returned...
For the time being, I guess I'll use query.list().size().

Sort items in cycle

I'm having trouble explaining what I am looking for so I will provide an example, let's say I have this array:
[
[1, 2],
[1, 3],
[1, 4],
[2, 3],
[2, 4],
[3, 4]
]
rather than sorting it by the first column, I would like it to cycle through the first column, so instead of 1, 1, 1, 2, 2, 3 it would do: 1, 2, 3, 1, 2, 1
resulting in:
[
[1, 2],
[2, 3],
[3, 4],
[1, 3],
[2, 4],
[1, 4]
]
Even better would be if it could cycle through both columns to prevent two numbers in a row as much as possible, the ideal solution would sort the original array as:
[
[1, 2],
[3, 4],
[1, 3],
[2, 4],
[1, 4],
[2, 3]
]
Leading to the maximum spacing between repeating numbers for each inner array (both columns being taken into account).
I hope I have provided sufficient information, and I will greatly appreciate any advise, I am fairly clueless so far, searching has yeilded me nothing.
I will only address the first part of your question as I don't understand what you mean by "Even better would be if it could cycle through both columns to prevent two numbers in a row as much as possible...". The clause "as much as possible" is especially troublesome, as it refers to an unspecified criterion.
Let arr be your array. The elements are sorted in your example, but if they were not, the first step would be:
arr.sort!
See Array#sort! and Array#<=> for an explanation of how Ruby sorts arrays whose elements are arrays.
There are many ways to obtain the desired ordering. Here is one that uses Enumerable#chunk:
arr.chunk(&:first).flat_map {|_,a| a.map.with_index {|i,b| [b,i]}}.sort.map(&:last)
#=> [[1, 2], [2, 3], [3, 4], [1, 3], [2, 4], [1, 4]]
The steps are as follows:
e = arr.chunk(&:first)
#=> #<Enumerator: #<Enumerator::Generator:0x007fa01a8141d0>:each>
We can see the elements of this enumerator, which are passed to the block by Enumerator#each (which calls Array#each), by converting it to an array:
e.to_a
#=> [[1, [[1, 2], [1, 3], [1, 4]]], [2, [[2, 3], [2, 4]]], [3, [[3, 4]]]]
Continuing:
f = e.flat_map { |_,a| a.map.with_index { |i,b| [b,i] } }
#=> [[0, [1, 2]], [1, [1, 3]], [2, [1, 4]], [0, [2, 3]], [1, [2, 4]], [0, [3, 4]]]
g = f.sort
#=> [[0, 1, 2], [0, 2, 3], [0, 3, 4], [1, 1, 3], [1, 2, 4], [2, 1, 4]]
g.map(&:last)
#=> [[1, 2], [2, 3], [3, 4], [1, 3], [2, 4], [1, 4]]
Let's look more closely at the calculation of f:
h = e.flat_map
#=> #<Enumerator: #<Enumerator: #<Enumerator::Generator:0x007fa01a8141d0>:each>:flat_map>
h.to_a
#=> [[1, [[1, 2], [1, 3], [1, 4]]], [2, [[2, 3], [2, 4]]], [3, [[3, 4]]]]
You can think of h as a "compound" enumerator.
The first value of h, [1, [[1, 2], [1, 3], [1, 4]]], is passed to the block and captured by the block variables using parallel (or multiple) assignment:
i, a = h.next
#=> [1, [[1, 2], [1, 3], [1, 4]]]
i #=> 1
a #=> [[1, 2], [1, 3], [1, 4]]
As i is not used in the block calculation, it is customary to replace that block variable with the local variable _.
We can now perform the block calculation:
a.map.with_index { |i,b| [b,i] }
#=> [[0, [1, 2]], [1, [1, 3]], [2, [1, 4]]]
The remaining calculations are performed similarly.
you could try this
def func ary
ret = []
# group by first ones, and each sort by second ones
a = ary.group_by{|i| i[0]}.map{|_,i| i.sort_by{|j| j[1]}}
# add to ret
(0...a.map{|i| i.size}.max).map{
a.map{|i| ret << i.shift}
}
ret.compact
end
a = [[1, 2],[1, 3],[1, 4],[2, 3],[2, 4],[3, 4]]
p func(a)
#=> [[1, 2], [2, 3], [3, 4], [1, 3], [2, 4], [1, 4]]
Assuming the initial array is sorted by the first element:
arr =
[
[1, 2],
[1, 3],
[1, 4],
[2, 3],
[2, 4],
[3, 4],
]
res = []
arr_dup = arr.dup
remaining_values = arr_dup.map { |el| el[0] }
current_value = remaining_values.first
loop do
arr_dup.each_with_index do |el, index|
if el[0] >= current_value
res << el
current_value = remaining_values.select { |v| v > el[0] }.first || remaining_values.first
remaining_values.delete_at(remaining_values.index(current_value))
arr_dup.delete_at(index)
break
end
end
break if remaining_values.empty?
end
p arr #=> [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
p res #=> [[1, 2], [2, 3], [3, 4], [1, 3], [2, 4], [1, 4]]
Few tests:
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [5, 1], [20, 2]] =>
[[1, 2], [2, 3], [3, 4], [5, 1], [20, 2], [1, 3], [2, 4], [1, 4]]
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [5, 1], [5, 2], [20, 2]] =>
[[1, 2], [2, 3], [3, 4], [5, 1], [20, 2], [1, 3], [2, 4], [5, 2], [1, 4]]

Array of 28 items doesn't work. Nothing happen from compiler

I'm trying write my iOS app with static data where the data will be saved in Array. But if I'm working with 4-5 items, is it OK, if is there more than 5, the compiler doesn't work. He is on the step like you can see on this screen:
And I need the informations next the name of items. Someone know where is problem, how I can fix it?
var people = [
[1, "Breta", 3],
[2, "Brunda", 3],
[3, "Antonin", 3],
[4, "Andolf", 3],
[5, "Barborka", 2],
[6, "Boruvka", 2],
[7, "Anicka", 2],
[8, "Antonin", 3],
[9, "Andolf", 3],
[10, "Barborka", 2],
[11, "Boruvka", 2],
[12, "Anicka", 2],
[13, "Antonin", 3],
[14, "Andolf", 3],
[15, "Barborka", 2],
[16, "Boruvka", 2],
[17, "Anicka", 2],
[18, "Antonin", 3],
[19, "Andolf", 3],
[20, "Barborka", 2],
[21, "Boruvka", 2],
[22, "Anicka", 2],
[23, "Antonin", 3],
[24, "Andolf", 3],
[25, "Barborka", 2],
[26, "Boruvka", 2],
[27, "Anicka", 2],
[28, "Andulka", 2]
]
You just need to declare it as [AnyObject]:
var people:[AnyObject] = [[1, "Breta", 3],[2, "Brunda", 3],[3, "Antonin", 3],[4, "Andolf", 3],[5, "Barborka", 2],[6, "Boruvka", 2],[7, "Anicka", 2],[8, "Antonin", 3],[9, "Andolf", 3],[10, "Barborka", 2],[11, "Boruvka", 2],[12, "Anicka", 2],[13, "Antonin", 3],[14, "Andolf", 3],[15, "Barborka", 2],[16, "Boruvka", 2],[17, "Anicka", 2],[18, "Antonin", 3],[19, "Andolf", 3],[20, "Barborka", 2],[21, "Boruvka", 2],[22, "Anicka", 2],[23, "Antonin", 3],[24, "Andolf", 3],[25, "Barborka", 2],[26, "Boruvka", 2],[27, "Anicka", 2],[28, "Andulka", 2]]
If you wait long enough it will finish, you just have to deal with it right now. Swift is extremely slow, this kind of stuff should be fixed in swift 1.2.
It's in beta right now with iOS 8.3.

Build and order an array with the first element of other arrays

I'm trying to transform an array of 3 arrays in an array of 4 where each array is built and ordered with the first element of each other.
I have this:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
And I would like the following:
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
Any ideas?
Use Array#transpose.
your_array.transpose
will do it.
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
array.transpose
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
Is this what you were looking for?

Sort array in Ruby on Rails

arr = [
[0, "Moving Companies", 10],
[0, "ab-thera-sensa", 5],
[0, "belt-center", 16],
[0, "isabel", 3],
[0, "kreatio", 2],
[0, "service1", 7],
[0, "sorbion-sachet-multi-star", 6],
[0, "sss", 15],
[0, "telecom-service-industry", 14],
[1, " AbsoPad", 13],
[1, "telecom-service", 8],
[2, "cutisorb-ultra", 12],
[2, "sorbion-contact", 11],
[2, "sorbion-sachet-multi-star", 9]
]
Suppose this is my array, now I want to sort it on the basis of the first element in descending order. I can do a arr.sort.reverse but the problem starts now
I get the array as :
[
[2, "sorbion-sachet-multi-star", 9],
[2, "sorbion-contact", 11],
[2, "cutisorb-ultra", 12],
[1, "telecom-service", 8],
[1, " AbsoPad", 13],
[0, "telecom-service-industry", 14],
[0, "sss", 15], [0, "sorbion-sachet-multi-star", 6],
[0, "service1", 7],
[0, "kreatio", 2],
[0, "isabel", 3],
[0, "belt-center", 16],
[0, "ab-thera-sensa", 5],
[0, "Moving Companies", 10]
]
Now the array should be sorted on the basis of the second element in ascending order.
How can that be achieved?
The result should look like :
[
[2, "cutisorb-ultra", 12],
[2, "sorbion-contact", 11],
[2, "sorbion-sachet-multi-star", 9],
[1,.......]
]
Customize the sorting with a block. First do a descending sort by the first element (0). If they are equal do instead an ascending sort by the second element (1):
arr.sort! do |a, b|
result = b[0] <=> a[0]
result = a[1] <=> b[1] if result == 0
result
end
How about this?
arr.sort { |i,j| [j[0],j[2]] <=> [i[0],i[2]] }
Outputs:
=> [[2, "cutisorb-ultra", 12],
[2, "sorbion-contact", 11],
[2, "sorbion-sachet-multi-star", 9],
[1, " AbsoPad", 13],
[1, "telecom-service", 8],
[0, "belt-center", 16],
[0, "sss", 15],
[0, "telecom-service-industry", 14],
[0, "Moving Companies", 10],
[0, "service1", 7],
[0, "sorbion-sachet-multi-star", 6],
[0, "ab-thera-sensa", 5],
[0, "isabel", 3],
[0, "kreatio", 2]]
please try:
arr.sort_by{|x|[-x[0],-x[2]]}

Resources