Crash while Mapping output to Core data in Restkit - iphone

I have 3 tables userID, profile which has to one relationship and searchId, profile has to one relationship. I set the delete rule to Nullify for both relationship. I have two View Controllers in which i am mapping the json data from server using Restkit.
In View Controller 1:
In this view Datas are fetched from Server and Mapped in DB using Restkit. (userID - > profile)
In View Contrller 2:
Data is retrieved from server and mapped into DB (searchID -> profile).
There is a refresh button in View Controller 1, which will fetch the data from server and map(update) the db.
Problem: After View Controller 1 loads the data from server, I am trying to search the data in View Controller 2 which loads the data from the server. I am Loading those data in ViewController 1 UI (Because View Controller 1 results and View Controller 2 Results are in Similar Format) No Problem here.
But When i Click the refresh button again, App got Crashed with Following Error. I can't able to figure out the problem.
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x8b4b950 <x-coredata://EE00CF63-BECD-40FC-B531-1424930D75D6/USERID/p42>''
*** First throw call stack:
(0x2b78022 0x2d28cd6 0x230b506 0x230b0a7 0x230ac86 0x2316db9 0x2316c26 0x231a38e 0x234a5f6 0x2338df7 0x233791e 0x233765d 0x2336f0a 0x1d59d91 0x1d59895 0x1d3f33e 0x231af3f 0x231a449 0x234a5f6 0x2338df7 0x23379ec 0x233765d 0x2336f0a 0x1d59d91 0x1d59895 0x1d3f33e 0x231af3f 0x231a449 0x234a5f6 0x2338df7 0x2338d64 0x1d70d50 0x273ebd 0x274727 0x1d7463e 0x1d6d1e7 0x1d6ceea 0x29c725 0x2a78b0 0x29e0f7 0x29e773 0x29ea5a 0x28ed330 0x28eef0c 0x28eecb4 0x28ee402 0x97dd7b24 0x97dd96fe)
terminate called throwing an exception
Any help is much appreciated

Are all your managed object contexts saved before you hit the refresh button? The "CoreData could not fulfill a fault" error means that Core Data was trying to find a row with a particular ID, but it could not find the row. This can happen when you pass managed objects across threads (which you should never do, you should pass their object IDs) or pass their IDs across threads without saving the context in which the object was created.

Related

Naming inputs / outputs functions / events in a View Model

For example, there's a View Model called PokemonTableViewModel which is used to populate a TableView with Pokemon data from the backend. It uses RxSwift for the input and outputs. There's an input function (well, a PublishRelay) that will trigger getting the data, and three output events:
Showing an activity indicator while getting the data
Load the data when the data is retrieved
Show an error message when the data retrieval failed
My questions are:
Which one is better to name the input?
Using the name of the action (eg. PokemonTableViewModel.getData)
Using the name of the View Controller's lifecycle method (eg.
PokemonTableViewModel.onViewDidAppear)
Which one is better to name the outputs?
Using the name of the actions that should be done by the View (eg. PokemonTableViewModel.showActivityIndicator, PokemonTableViewModel.populateTable, PokemonTableViewModel.showErrorMessage)
Using the name of the actions that triggered the events (eg. PokemonTableViewModel.onDataLoading, PokemonTableViewModel.onDataRetrieved, PokemonTableViewModel.onDataFailed)
Thanks.

Firebase Cloud Firestore - Initializing a Collection [duplicate]

This question already has an answer here:
How to create a Collection in Firestore with node.js code
(1 answer)
Closed 4 years ago.
I'm developing an chat app in Swift and when the user creates a chatroom for the first time, it obviously doesn't exist yet in Firebase. Only until the first message gets created does everything build out.
When my app first loads, in the viewDidLoad(), I'm setting a reference to the chat at this path (keep in mind it doesn't exist):
reference = db.collection("chatrooms").document("\(chatRoomId)").collection("thread").document("\(threadId)").collection("messages")
So when I add my snapshot listener just below this, and the user creates his first message on the thread, the listener doesn't fire for everyone (I assume, anyway, because it doesn't fire for me)
I do like that once you add your first message, it creates the whole document structure above, but this means my first users who might be watching the thread (however unlikely this is) won't get the message.
Is this an uncommon edge case I'm trying to guard against, or do I need to do something like this (pseudo-code)?
IF (this collection doesn't exist yet) THEN
CREATE GROUP/THREAD/EMPTY MESSAGE COLLECTION
reference = db.collection("chatrooms").document("\(chatRoomId)").collection("thread").document("\(threadId)").collection("messages")
ELSE
reference = db.collection("chatrooms").document("\(chatRoomId)").collection("thread").document("\(threadId)").collection("messages")
END
I was wondering if there is some sort of initializer I can call to make the structure for me, but still set it up so my first message to the collection fires the snapshot listener.
Thanks!
What you're doing is unnecessary. There is no formal method required to create a collection. A collection simply springs into existence when you first write a document into it. You can listen on an empty collection with no problems, and listeners should be invoked even if the collection wasn't visible in the dashboard at the time it was added.

Queuing multi-tenant application in Laravel

I have a multi-tenant application built in Laravel 5.1. It uses one main database connection for storing users, roles, permissions, as well as jobs and failed_jobs. Additionally, every user has his own database.
I use a Job class for sending mail, and when it is executed, the following exception occurs:
[PDOException] SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
The class uses tables from two db connections (the main connection, and the one associated with the current user).
Thanks in advance, any help is appreciated.
Ok, this was easy. For anyone who is interested, I totally forgot to set the database in the second connection.
In fact, the database field in the second db connection is dynamically filled, depending on the authenticated user. So, each time the job class is executed, there should be initialization of the database field:
Config::set('database.connections.second_connection.database', 'user_' . $user_id);
// $user_id is in fact auth()->user()->id, passed as parameter
That solves the problem.

How do I pretend duplicate values in my read database with CQRS

Say that I have a User table in my ReadDatabase (use SQL Server). In a regulare read/write database I can put like a index on the table to make sure that 2 users aren't addedd to the table with the same emailadress.
So if I try to add a user with a emailadress that already exist in my table for a diffrent user, the sql server will throw an exception back.
In Cqrs I can't do that since if I decouple the write to my readdatabas from the domain model, by puting it on an asyncronus queue I wont get the exception thrown back to me, and I will return "OK" to the UI and the user will think that he is added to the database, when infact he will never be added to the read database.
I can do a search in the read database checking if there is a user already in my database with the emailadress, and if there is one, then thru an exception back to the UI. But if they press the save button the same time, I will do 2 checks to the database and see that there isn't any user in the database with the emailadress, I send back that it's okay. Put it on my queue and later it will fail (by hitting the unique identifier).
Am I suppose to load all users from my EventSource (it's a SQL Server) and then do the check on that collection, to see if I have a User that already has this emailadress. That sounds a bit crazy too me...
How have you people solved it?
The way I can see is to not using an asyncronized queue, but use a syncronized one but that will affect perfomance really bad, specially when you have many "read storages" to write to...
Need some help here...
Searching for CQRS Set Based Validation will give you solutions to this issue.
Greg Young posted about the business impact of embracing eventual consistency http://codebetter.com/gregyoung/2010/08/12/eventual-consistency-and-set-validation/
Jérémie Chassaing posted about discovering missing aggregate roots in the domain http://thinkbeforecoding.com/post/2009/10/28/Uniqueness-validation-in-CQRS-Architecture
Related stack overflow questions:
How to handle set based consistency validation in CQRS?
CQRS Validation & uniqueness

How can I best deal with problems when initializing a model in an iphone app?

So assume I have a class that has an init method that does something like... grabs some data off the net in xml format and parses it to initialize some of its properties. My concern is how should I handle the case where the network is down or the xml data my object receives is bad?
Normally in C i would use return values to indicate an error and what kind and then that would get propagated back till I could report it to the user. I don't really think that will work in this situation.
Use asynchronous network requests.
Create the UI and show it with
either dummy replacement for the
actual values (like pictures) or no
data (for example empty table).
Then create and send the request for
data and register handler that gets
called with data.
When you receive data your handler
gets called with them.
You parse the data and update the
UI. In case of data being invalid
you can now update UI to inform the
user.
You can use timeouts to cancel
requests in case of network problem
and functions not returning with
data within specific time.
There was an example in last year Stanford's CS193p class (iPhone programming but the same applies to desktop apps) with showing empty user interface and updating it when data coming back. You can probably find references to it on net or otherwise there'll be new example this year.
For network down you have a few options
Alert the user you cannot retrieve the needed data
Show stale (last loaded, maybe not stale?) data
For Bad Data:
Alert the user
Try again
Show old data
Try to fix the data (missing a closing tag? etc)
Show a subset of the data (maybe you can extract something that is usable?)
As far as error codes, you can do:
Return codes ie bad_data -1, no_network -2, etc.
You can throw exceptions, catch them and map them to user friendly display messages