How to keep variables available to cross check when used later

Hello:


I want to be able to check if a specific Core Data record was fetched previously by comparing the Record ID with the one being currently fetched. I want to some how to mark each fetched record so I can know whether or not it has been called before.


I don't want to save every fetched record to a data store then compare new fetches with what is there because it would call for a lot of processing resources.


Is there an easier way?


Thanks.

Accepted Reply

OK, if you can fetch all questions (but what about when you have 1000 or 10 000 ?), you can do as you propose.


As for creating the dict, you need first to select what should be the unique key. RecordID string seems well appropriate.


var alreadyAnswered = [String: Bool]()


It is probably wiser to keep track of answered questions, not of all fetched.

-> In the code where question is presented to user:

let questionID = recordID // or String(recordID) depending what recordID is
alreadyAnswered[questionID] = true


Then, before presenting a new question:

- you have to fetch or you read from the fetched values

let questionID = recordID // or String(recordID) depending what recordID is

if !alreadyAnswered[questionID] {
     // show the question
     alreadyAnswered[questionID] = true
}

Replies

To understand, why do you want to know if it was fetched ?


A very simple way would be

- if you know the number or records, to create an array of Bool where you set to true for each record fetched.

- or create a dictionary, where the key is RecordID and the value Bool, true or false if record was fetched or not.

Hello Claude31:


In my app, the user gains points by providing correct answers to questions presented. I don't want the user to Game the system by continuously saving a correct answer to increase his score. So if the question was fetched earlier, and is saved again, the user will get no points the second or third time around. So in my logic, I have to code that if the same question (by looking at the Qiestion ID) is saved, the value will be 0 points.

OK, clear.


So, the dictionary option is appropriate.

The key may be a String derived from recordID, or a question number (String), or whatever is unique.

Hello Claude31:


Thanks.


I am thinking of another solution that may have an advantage. That is, Since I loaded all the records I want to use into a temporary Entity, I can delete the record that was fetched. (This entity is refreshed each time the user changes certain predicates).


However, I would like to create the dictionary as you suggested, but I've not created one before manually (I've used in Core Data to find UNIQUE records, but core data does the work).


Could you provide a sample code?


(I'll accept the last suggestion as the correct answer.)

OK, if you can fetch all questions (but what about when you have 1000 or 10 000 ?), you can do as you propose.


As for creating the dict, you need first to select what should be the unique key. RecordID string seems well appropriate.


var alreadyAnswered = [String: Bool]()


It is probably wiser to keep track of answered questions, not of all fetched.

-> In the code where question is presented to user:

let questionID = recordID // or String(recordID) depending what recordID is
alreadyAnswered[questionID] = true


Then, before presenting a new question:

- you have to fetch or you read from the fetched values

let questionID = recordID // or String(recordID) depending what recordID is

if !alreadyAnswered[questionID] {
     // show the question
     alreadyAnswered[questionID] = true
}

Great!


Thanks again.