I have some code that is running very slow. I narrowed down the issue to the following lines. If commented out, the code runs rapidly. If present, this process can take 30 seconds or more although no error is reported by the compiler or at runtime.
I have a date in a temporary object as follows:
NSDate *lastedited = importObject.lastedited;
If I log this to the console it logs as:
2015-10-08 08:44:51 +0000
The above does not cause a delay
When I then go to save it in Core Data, I first check if it is null. However, the following code is what causes the extreme delay:
if (lastedited != (id)[NSNull null]){
[record setValue:lastedited forKey:#"lastedited"];
}
Edit
I have discovered that even taking out the test, the line [record setValue:lastedited forKey:#"lastedited"] runs extremely slowly.
This is despite the fact that lastedited in the entity is an NSDate in the data model.
And as the line at the top shows it is in the form of an NSDate and logs to the console as 2015-10-08 08:44:51 +0000
What might be causing the code to run so slowly?
Is there a reason you're not simply testing against nil?
if (lastedited != nil) {
....
}
Here's a good overview of the different kinds of nothing in Objective-C/Cocoa: http://nshipster.com/nil/
I think the issue is with the data type of lastedited field in your Coredata entity. The data type you are using is NSDate, which is of course not a primitive data type supported by Coredata. If the data type is not one of the primitive types (like int, float, double, String, NSTimeInterval), Coredata will first convert it into one of the primitive types. In your case, the date is converted to NSTimeInterval just before saving, which is a bulky process and takes more time than expected. So if you are doing the overhead of converting the NSDate to NSTimeInterval before saving, the process will become faster. Try this
[record setValue:([lastedited timeIntervalSince1970] * 1000) forKey:#"lastedited"];
Don't forget to change the data type of lastedited to NSTimeInterval in .h file of your entity.
#property (nullable, nonatomic, copy) NSTimeInterval *lastedited;
Keep in mind that you are saving the date as milliseconds to coredata now. So whenever you are fetching it from coredata, convert it to NSDate and use
Related
I'm facing an issue if I pass NSDate as a function parameter.
My code is:
self.PassDate(responseDate) ; // Response date value = 2015-05-15T00:00:00
func PassDate(date:NSDate) {
// Here ideally date value should be same as responseDate value.. but date is coming as 2015-05-14T07:00:00 PDT
}
Why passing NSDate is changing the timezone/values?
Nothing is changing. Different ways of viewing dates present them using different time zones. The underlying date is not changed. If you view the date in the debugger it (usually) shows it in UTC.
Try logging the date outside your function and inside using println() calls. You should see the same value in both places if you view it the same way each time.
I have stored NSDate in the DB using http://www.appcoda.com/sqlite-database-ios-app-tutorial/.
When i fetch the date back to NSString i get following during debugging. (As mentioned in tutorial even though i have a date picker and store value in NSDate, its actually stored as string in DB finally.
(lldb) po _itemPurchaseDate
(
)
Attached screenshot. So how do i fetch date and present it in a ui label in iOS?
How does one insert a row into a table that I've created with two sqlserver smalldatetime fields via the windows azure mobile services library? I'm currently trying to use:
[NSDate timeIntervalSince1970]
to store the date-time value as an NSTimeInterval or double. However, this causes the following error:
Error Domain=com.Microsoft.WindowsAzureMobileServices.ErrorDomain Code=-1302 "Error: Bad request."
DateTime fields should be represented by an NSDate field in the NSDictionary object you are sending to insert or update call.
So you would just do:
[table insert:#{ #"id": #"myid", #"deliveryDate": [NSDate date] } completion:... ];
If you send an NSNumber instead, then you would see the -1302 error shown above.
I am pulling data from the web that is formatted in JSON and when I parse the data using "ValueForKeyPath" it stores the string value as an id object.
So I store the data into a NSMutableArray
In the debugger window it shows all the elements added as (id) null.
I have an if statement
if ([[self.activeCategories objectAtIndex:selected] boolValue] == true)
Sometimes I would say 20% of the time it fails the if statement when it should not.
I was wondering if it was because the self.activeCategories is storing id types. Do I need to do [NSString stringWithFormat#"%#", x] to all the objects inside the array? It seems like when I just straight cast it by using (NSString *) it is still type id in the debugger.
It's a very strange error to me... as the error is not consistently reproducible.
Try it like that:
if ([[self.activeCategories objectAtIndex:selected] boolValue])
According to that article a BOOL may hold values other than 0 and 1 which may fail the comparison.
Hia, got a one to many relation for chars and items. one char can hold a specific item, same item can be used by others.
The CharInfo is defined as follows:
#property (nonatomic, retain) ItemInfo * slotEar;
CharInfo.slotEar is a reference to the item. It is optional, min count 1, max count 1 and delete rule Nulify.
ItemInfo is defined as:
#property (nonatomic, retain) NSSet* slotEar;
ItemInfo.slotEar is a reference to the char. It is optional, one to many and delete rule Nulify.
They are referencing to each other.
There is an additional class that works with the data. It does hold the reference as well and provide it for storing.
ItemInfo *slotEar;
CharInfo get created before saving like this:
When I save the CharInfo, I set the ItemInfo (from my structure) in the aproviate slot.
CharInfo *charInfo = [NSEntityDescription
insertNewObjectForEntityForName:#"CharInfo"
inManagedObjectContext:managedObjectContext];
charInfo.slotEar = currentChar.slotEar;
Saving the context works.
When I try to load the CharInfo from store, it works most of the time from now. After relaunching he does crash at this line.
curentChar.slotEar = charInfo.slotEar;
If there was no item reference (nil) then all is fine.
Unfortunately the crash is more a halt. No error is given, he just stops at that line in the debugger and the green description next to the link says: EXC_BAD_ACCESS
Seems something is wrong with the reference I save or the way how I try to take it from the CharInfo to my class. Any idea?
Screenshot added:
No bug with core data or the worker class ivars, but with an int array with 6 ints filled with 100 ints.