title: Dynamically fetch data for plot range in core plot iOS
body: <p>I am writing an application that records some data for an extended period of time (up to several hours) and displays the data in a graph/plot. I am storing my recorded values using core data and using core plot to display them in a scatter plot. My Core data entity looks like this:</p>
  
  <p>MyEntity</p>
  
  <ul>
  <li>timeStamp:Date </li>
  <li>someValue:Integer 64</li>
  </ul>
  
  <p>My core plot graph is a scatter plot that displays the "someValue" values on the y axis and the "timeStamp" values on the x axis.</p>
  
  <p>The screenshot shows what the core plot looks like.</p>
  
  <p><img src="https://i.stack.imgur.com/N0UAF.png" alt="My core plot graph with &quot;someValue&quot; on the y-axis and &quot;timeStamp&quot; on the x-axis."></p>
  
  <p>The problem is I have a lot of values (values are being added to the core data data base every second for a couple of hours). I don't want to fetch all of my Core Data entities and load them into memory to be displayed in core plot at once. Instead, I'd rather only fetch the entities that lie within the visible plot range from my core data store and then display them using core plot. I would store the fetched results in an NSArray or something similar and would use the NSArray in my CPTPlotDataSource methods to populate the plot. My NSArray would always get overwritten when the range changes so that only the visible data points are kept in memory.</p>
  
  <p>What I thought was I would use the</p>
  
  <pre><code>- (CPTPlotRange *)plotSpace:(CPTPlotSpace *)space
    willChangePlotRangeTo:(CPTPlotRange *)newRange
            forCoordinate:(CPTCoordinate)coordinate
  </code></pre>
  
  <p>method of the CPTPlotSpaceDelegate, and fetch the relevant entities from core data using the timeStamp as a predicate. However, I couldn't figure out how to do that. Is there a way to access the timeStamp values given the plot's current range? I thought about trying to access my x-axis labels since they display the current time and fetching from my data store using that but I couldn't figure out how. Is this even the right way to go about this? Any help is appreciated.</p>