CloudKit, NSPredicate to return a count or determine if any records exists , in a private container? - objective-c

I've been researching how to determine is any RecordType records exists in a Private Container, perhaps from a previous app installation or from another device in the users iCloud account.
I see that you can not perform an NSPredicate count of records.
However I can't find an alternative to find if any records exists?

There is no way to get a count.
If you wish to determine if there are any records for a given record type, perform a CKQueryOperation for the given record type. Set the query's predicate to [NSPredicate predicateWithValue:YES] and set the operation's resultLimit to 1.
Then check the results. You'll either get one row back if there are any records or you'll get no rows back (or possible an error, see what happens).

Related

Laravel: Retrieve the one-and-only record from database

How can I retrieve the one-and-only record from the database w/o Model::all();?
I do not know it's id either so I can't use Model::find($id);.
You can use the first() method to get the first record from the database matching additional conditions if any are included:
$record = Model::first();
This will return an instance of Model, or null if there are no records present.

CDC LSNs: queries return different minimum value

I have CDC enable on a table and I'm trying to get the minimum LSN for that table to use in an ETL job. However when I run this query
select sys.fn_cdc_get_min_lsn('dbo_Table')
I get a different result to this query
select min(__$start_lsn) from cdc.dbo_Table_CT
Shouldn't these queries return the same values? and if they don't why not? and how to get them back in sync?
The first query:
select sys.fn_cdc_get_min_lsn('dbo_Table')
Invokes a system function that returns the lowest POSSIBLE lsn for the capture instance. This value is set when the cleanup function runs. It is recorded in, and queried from, cdc.change_tables.
The second query:
select min(__$start_lsn) from cdc.dbo_Table_CT
Looks at the actual capture instance and returns the lowest ACTUAL lsn for the instance. This value is set when the first actual change to the instance is logged after the cleanup function runs. It is recorded in, and queried from, cdc.dbo_Table_CT.
They're unlikely to tie out, statistically speaking. For the purposes of an ETL job, using the call to the system table will likely be quicker, and is a more accurate reflection of when the current set of change records started being accumulated.

Iterate over a task model, counting users, ...then remove any user who exists more than once

I need to evaluate if a user_id exists more than once in an array. Ultimately, I need to determine if a user was able to complete a task (each task is saved as a record) in one attempt. I need to display a percentage of success, which is ultimately determined by the number of users who get it right on the first try. I added a boolean to the task model ':passed', but then I have to write more logic to set that boolean for the first record, and then unset it if any subsequent record is created. That smells. My approach now is to simply create an array of task.users, then determine if any user_id exists in that array more than once...and if it does remove all instances of that integer from the array (so that they are not counted). I'm tripping over my own thought process and not having success...
How can I iterate over all tasks and count each iterations user_id and .delete(user_id) of any user whos count is > 1?
Don't understand what you're asking, but this will give you an array of user_id's that have completed the task without duplicates:
task.users.map(&:user_id).uniq
Based on your comment you can try:
task.users.group_by{ |u| u.user_id }.collect{ |u, dups| u if dups.size == 1 }.compact

Selecting specific joined record from findAll() with a hasMany() include

(I tried posting this to the CFWheels Google Group (twice), but for some reason my message never appears. Is that list moderated?)
Here's my problem: I'm working on a social networking app in CF on Wheels, not too dissimilar from the one we're all familiar with in Chris Peters's awesome tutorials. In mine, though, I'm required to display the most recent status message in the user directory. I've got a User model with hasMany("statuses") and a Status model with belongsTo("user"). So here's the code I started with:
users = model("user").findAll(include="userprofile, statuses");
This of course returns one record for every status message in the statuses table. Massive overkill. So next I try:
users = model("user").findAll(include="userprofile, statuses", group="users.id");
Getting closer, but now we're getting the first status record for each user (the lowest status.id), when I want to select for the most recent status. I think in straight SQL I would use a subquery to reorder the statuses first, but that's not available to me in the Wheels ORM. So is there another clean way to achieve this, or will I have to drag a huge query result or object the statuses into my CFML and then filter them out while I loop?
You can grab the most recent status using a calculated property:
// models/User.cfc
function init() {
property(
name="mostRecentStatusMessage",
sql="SELECT message FROM statuses WHERE userid = users.id ORDER BY createdat DESC LIMIT 1,1"
);
}
Of course, the syntax of the SELECT statement will depend on your RDBMS, but that should get you started.
The downside is that you'll need to create a calculated property for each column that you need available in your query.
The other option is to create a method in your model and write custom SQL in <cfquery> tags. That way is perfectly valid as well.
I don't know your exact DB schema, but shouldn't your findAll() look more like something such as this:
statuses = model("status").findAll(include="userprofile(user)", where="userid = users.id");
That should get all statuses from a specific user...or is it that you need it for all users? I'm finding your question a little tricky to work out. What is it you're exactly trying to get returned?

tests for data access (sql queries) functionality

I want to know the best way of testing data access functionality.
I know that it is possible to create mocks to change data layer objects that is using to test business logic.
However is it possible to test if sql queries to database are correct.
Scenario: A method must return employees which were applied for work last month.
I can return list of object and check if each employee's startDate property is correct (last month).
So if it returns 3 employees and they have correct startDate value but there are more two employee in database which aren't returned. How to write test for that case? :)
Thanks in advance.
You set up the test DB so you shold know what data is in it. If you expect 5 employees to be returned from the query and you get only 3, you know there is an error.
You can test the query with different setups: empty table, only new employees, only old employees, a mix of the two (with special care to the borderline cases), etc.
I don't think you need to check the two other employees in the database which aren't returned.
The key is, when setting up your test data, you would want to ensure you have enough records that don't match the criteria (in addition to the records that do match), then you run the fetch and make sure you get back the correct number of records that do match the criteria.
Preparing the test data in this manner ensures that your method is returning the expected results.