UA-17470720-3

Jump to content


Photo
- - - - -

Using NSManagedObjects in a SCArrayOfObjectsSection


  • Please log in to reply
10 replies to this topic

#1 Gerald K.

Gerald K.

    Sr. Member

  • STV 5.0 Std
  • PipPipPip
  • 40 posts
Reputation: 5
Good

Posted 09 June 2015 - 12:59 AM

I know this might sound like a backwards design decision but bare with me:

 

Is it possible to use NSManagedObjects (in an array) as items of an SCArrayOfObjectsSection? We are in the process of transforming an application to also use local storage besides being connected to a web service. We replaced our NSObject model classes with a core data model but handle the saving / loading of objects on our own.

 

We now face the problem that although objects load just fine, when we set an array of NSManagedObjects as the items of the SCArrayOfObjectsSection nothing is displayed by STV. Is there any limitation in place, such as STV not being able to read/write values from NSManagedObjects when initialized via a SCClassDefinition instead of an SCEntityDefinition?

 

 

Thank you

Gerald



#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 09 June 2015 - 06:31 AM

Hi Gerald,

 

Thank you very much for reporting. This will be fixed in our very next release.



#3 Gerald K.

Gerald K.

    Sr. Member

  • STV 5.0 Std
  • PipPipPip
  • 40 posts
Reputation: 5
Good

Posted 09 June 2015 - 12:12 PM

Hi Tarek,

 

thank you for your quick reply.

 

Is there any chance I could get a preview of the changed version or does it involve a lot of changes on your side?



#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 June 2015 - 06:37 AM

Hey Gerald,

 

After deeper inspection of the issue, it turns out it's not STV's fault after all :)

 

What's happening is that SCClassDefinition is not finding the property names you're giving it when it inspects the "NSManagedObject" class, so it throws warnings and refuses to create these properties. A very simple fix is to have Xcode automatically create an NSManagedObject subclass for your entity, then give that subclass to SCClassDefinition instead of [NSManagedObject class]. To automatically create an NSManagedObject subclass: select your Core Data model in Xcode, then select "Editor->Create NSManagedObject Subclass..." from the menu and select your entity there.

 

Hope this helps.


  • Gerald K. likes this

#5 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 571 posts
  • LocationThe Grid
Reputation: 145
Popular

Posted 10 June 2015 - 07:14 AM

Greetings Programs!

 

Hey Tarek, not sure if this is what you meant but I tried this yesterday with the CoreDataApp, creating the subclasses and adddng the SCClassDefinition for each one of them.

 

When I ran it, STV displayed the properties just fine; I was able to dispatch events, add/modify values but they would not commit. In creation I got these STV warnings:

 

2015-06-10 08:57:20.665 CoreDataApp[17505:5032883] CoreData: error: Failed to call designated initializer on NSManagedObject class 'TaskEntity' 
2015-06-10 08:57:20.686 CoreDataApp[17505:5032883] **STV** Warning: Property 'category' does not exist in object 'TaskEntity'.
2015-06-10 09:02:24.555 CoreDataApp[17586:5035098] **STV** Warning: Property 'assignedTo' does not exist in object 'TaskEntity'.
[etc for all the properties]

 

And these when it would try to commit:

 

2015-06-10 09:04:14.789 CoreDataApp[17586:5035098] **STV** Warning: Property 'name' does not exist in object '<TaskStepEntity: 0x7f8f73531590> (entity: <null>; id: (null) ; data: <fault>)'.
2015-06-10 09:04:14.789 CoreDataApp[17586:5035098] **STV** Warning: Property 'details' does not exist in object '<TaskStepEntity: 0x7f8f73531590> (entity: <null>; id: (null) ; data: <fault>)'.

 

Wg


P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 June 2015 - 07:17 AM

hmmmm, I am not getting these warnings at all. Would you kindly post your code?



#7 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 571 posts
  • LocationThe Grid
Reputation: 145
Popular

Posted 10 June 2015 - 07:48 AM

Hi Tarek,

 

Here's the project:

 

https://www.dropbox.... Class.zip?dl=0

 

Wg


P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#8 Gerald K.

Gerald K.

    Sr. Member

  • STV 5.0 Std
  • PipPipPip
  • 40 posts
Reputation: 5
Good

Posted 10 June 2015 - 09:52 AM

Hey Tarek,

 

thanks for your reply. I already had custom subclasses for the NSManagedObjects. For example I have an "Account" class that subclasses NSManagedObject. And I also didnt get the warnings you mentioned, STV is just silent and still doesn't load the content of the cells. But I will experiment some more tomorrow.

 

Now today I tried to rewrite the whole app to use NSEntityDefinitions instead and got an interesting error which had to do with XCode not loading files from the STVCoreData library that only contained categories. After adding a linker flag (-load_all) it now works. It is also the cleaner solution.

 

Now if you could respond to one other question that came up today: After loading a view, I initially have no objects in coredata as they have to first be fetched from the web. Which action method would you recommend where STV lets me know that it would now load data from Core Data? Where I can block that load until my webservice fetched the initial dataset.

 

Thanks!



#9 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 571 posts
  • LocationThe Grid
Reputation: 145
Popular

Posted 10 June 2015 - 10:40 AM

Hi Gerald,

 

In my service app, when I start the web service call, I use SVProgressHUD to show an activity indicator  which disables user interaction. Once the web service call has completed, I either hide the activity indicator (if there are no results returned) or I send a notification to reload the tableview. Then, in the new modelActions.didFinishLoadingCells, I hide the activity indicator.

 

You can use the modelActions or sectionActions .didFetchItemsFromStore to let you know that the data has been retrieved from Core Data before being displayed.

 

Wg


  • Gerald K. likes this

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#10 Gerald K.

Gerald K.

    Sr. Member

  • STV 5.0 Std
  • PipPipPip
  • 40 posts
Reputation: 5
Good

Posted 10 June 2015 - 11:07 PM

Hey wizgod, tarek,

 

thanks for your input thus far. After some head-scratching I found out that we did not cast the NSManagedObjects properly after retrieving them from the database. Basically the executeFetchRequest:error: function only returns an array of NSManagedObjects (of course) and we assigned this array directly to the items array of our SCArrayOfObjectsSection (the one with an SCClassDefinition of NSManagedObject subclasses).

 

STV did not know what to do with those NSManagedObjects, so after figuring out that I needed to cast them to proper (Account*) objects STV started working again and is now filling the cells just like it did before I switched from NSObjects to NSManagedObjects.

 

:)


  • Tarek and wizgod like this

#11 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 11 June 2015 - 07:39 AM

Thanks for the update Gerald, glad it's working out now :)

 

@Wg: Thanks a lot for the app. The problem you have there is that you're starting with an empty dataset and you're having STV create the first "TaskEntity" object, which fails of course since you're creating a Core Data object using the wrong data definition (SCClassDefinition instead of SCEntityDefinition). What me and Gerald are doing is that we're fetching existing Core Data objects and simply displaying them. Our code would still fail and give warnings if we try and create new objects using the SCClassDefinition we created.


Edited by Tarek, 11 June 2015 - 07:39 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users