googleads / google-ads-ruby

Google Ads API Ruby Client Library
https://developers.google.com/google-ads/api/
Apache License 2.0
70 stars 67 forks source link

Unexpected enumerable response behaviour #290

Open apneadiving opened 3 years ago

apneadiving commented 3 years ago

Using v6 of the api and the search service, using the same search_query and customer_id in both scenari.

response = search_service.search(customer_id: customer_id, query: search_query)
response.count
=> 1
response.first
=> nil
response.to_a
=> []

response = search_service.search(customer_id: customer_id, query: search_query)
response.any?
=> true
response.first
=> <Google::Ads::GoogleAds::V6::Services::GoogleAdsRow: ...
response.to_a
=> [<Google::Ads::GoogleAds::V6::Services::GoogleAdsRow ...
response.to_a
=> []

I think the query is irrelevant here, the points are:

mcloonan commented 3 years ago

Not all the results are returned immediately, and there may be more requests made behind the scenes as you iterate through the results. Calling anything that will force an iteration through all results will effectively exhaust the iterator. So if you want to process the results and also do one of these kinds of operations, you'll need to do both together in a single pass.

I don't think there's anything we could do to fix the behavior, but I will look at trying to update our documentation for the library to explicitly mention this.

apneadiving commented 3 years ago

I understand the ins and outs. Thank you