Showing random data from core-data using Swift 3 - ios

I need some help getting random data from core data using Swift 3, Xcode 8.3.1. I currently have an app that creates a list in tableview using data that is entered by the user.. (user enters a name and takes a picture of that person) The entity "Friend" holds the attributes "name", "image".
The first version of this app was just a name and I would use arc4random to randomly update a label with a name on a modally presented VC on a button click. The names were simply stored in an Array.
This version is including an image so I decided to try my hand at core-data (never used it before) and now I'm stuck at my random select button. Currently the app will store the data fine and then retrieve it and display everyone alphabetically along with their image in a tableview. As a new person is submitted the info gets stored and the tableview updates.
I need to show a randomly selected name and its image, but I don't know how to do this and research has failed me on getting it done.
If there is a better way of storing an image & name instead of core-data I'm open to changing as well. The app stores anywhere from 20-80 different names. It will never be used to store much more than that.

You can fetch your items from the context, which will give you an array of objects. Now you just use your favorite random function to get a random index for this array. And then use an object at that index.

Related

When I try to save data to Firebase it only updates the existing data, Swift, Firebase

The way I set up my database structure was like this:
It starts with Lists then there is a child that shows the users UID then inside that there is one item.
The one item inside the UID gets updated every time I attempt to save new data. Instead of adding another item the same one just keeps changing. I was wondering how I could instead of update the same one item every time add more items.
The way that I save my data is with this line of code.
let user = FIRAuth.auth()?.currentUser
let item: String = self.ItemTextField.text!
self.ref.child("Lists").child(user!.uid).setValue(["Items": item])
More idiomatic is to store the list of items with so-called push ids:
Lists
twEymn...
-Km....: "Yoghurt"
You'd do this with:
self.ref.child("Lists").child(user!.uid).childByAutoId().setValue(item)
The childByAutoId() generates a unique, sequential ID. It's similar to an array index, but this one works reliably in multi-user environments and when users can be offline. Read this blog post about these so-called push ids.
Alternatively you can use the name of the item as the key (if the item has to be unique in the list):
Lists
twEymn...
"Yoghurt": true
In that case the code becomes:
self.ref.child("Lists").child(user!.uid).child(item).setValue(true)
One thing you'll note is that both of these approaches only deal with the newly added item, instead of the list of items as a whole. This is a general pattern you'll see when using Firebase. By isolating your modifications, your app will be more scalable without users getting into each other's way.
The problem is that you are setting a key-value pair ("Items" : item) so that each time it is updating the value for the same key. What you could do instead is ("Items" : [your array of items here]), which will update a list of items for the same key each time.
You could also fetch the current list of items, append your new item locally, and then update.

How to properly use createOrUpdateInRealm?

I am using Realm to handle persisting data to disk in my app. I am storing an ArticleCollection object to Realm (this just contains an array of NewsArticle objects).
I am using createOrUpdateInDefaultRealmWithValue to either create a new ArticleCollection or update the existing one (achieved using a primary key on ArticleCollection).
This currently works well when the app fetches new articles it will overwrite the current collection of articles and replace with a new instance of ArticleCollection.
The issue is that whilst ArticleCollection is replaced (in Realm brwoser I can see the count of ArticleCollection is always 1 as expected), the number of NewsArticles always increases. It seems that sub objects of ArticleCollection (NewsArticle) never get replaced/deleted, only appended to.
How can I ensure that when I use createOrUpdateInRealm, it will delete all sub objects?
Make sure that are also using a primary key on your NewsArticle object. Otherwise those news articles won't be updated. That's why you are seeing an increasing number of news articles.

iOS Swift - TableView data source - saving data when app close

I'm working on an swift application that uses a TableView. It's cells are filled-up with data that comes from a remote server. That's how my app works now, but I want to change this mode because I'm sure that it's not the correct way to do things:
First: when the tableview did load, I call a function that get data (json) from server with this method:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
After that, I save my data as an object in NSUserDefaults. The object contains strings such as username, description, location, picture... etc. I know that this is not correct to store images in NSUserDefaults even if these images are not so big...
Second: I fill up table's cells with data obtained from NSUserDefauts, where I have stored my json data.
First of all I want to know how to auto append data to the table when the user hits the last cell of the table, like all apps that uses tableviews for fetching user posts...
In Instagram, after the you close the app, disconnect from the internet, and reenter the app, it will show you the first 10 cells from the last table you've seen before closing the app, so Instagram saves somehow, somewhere, maybe in coredata, data for the first 10 cells.
How do you suggest me to do this?
Question is not particular.....
Load Data error.
Or you fetch the result from coreData error?
CoreData can save View and some informations.
Post your code.

Parse, How to send array entries as multiple PFObjects, Create a new row for each array object

I'm using Parse as the backend for my app. My app will be used in the field where service will nonexistent or spotty at best so I need to store information offline. I currently save data for the user in a plist in the background (Title, location coordinates, notes, additional data). Since Parse's current iOS offline saving is fairly poor (From what I've read), I was hoping to get around it by creating an array or dictionary from the plist and upload that to Parse by giving it an array once the user is back in cell range.
As it occurs now, when I upload the array, it simply puts the entire contents of the array in a single cell in the database. Is there a way to parse the array and create a new row for each entry/object in the array?
I may have overlooked a better way to do this. If someone has a suggestion I would appreciate it!
I solved it. I iterated through the array using a for loop and added each index as a separate object.

Pictures and videos in a tableview for each user

My app will have a tableview with a picture and a title on each row, when the user taps it, it will open another view with that title, some texts and some pictures.
Now, I want each user to have different items in the tableview. For making it easier, suppose it is going to be a todo list like this:
Wash the car (Picture1) (video1) (InstructionsText)
Go to the groceries store (Pic2) (Vid2) (instrText2)
Pay the Bills (Pic3) (Vid3) (instrText3).
Now, I want to assign these items differently for each user, and show in a tableview.
USER 1 TABLE VIEW
Wash the car (Picture1) (video1) (InstructionsText)
Pay the Bills (Pic3) (Vid3) (instrText3).
USER 2 TABLE VIEW
Pay the Bills (Pic3) (Vid3) (instrText3).
USER 3 TABLE VIEW
Wash the car (Picture1) (video1) (InstructionsText)
Go to the groceries store (Pic2) (Vid2) (instrText2)
...
Now, here's my idea: each of these tasks should open a view controller with pictures, videos and text related to them. Using Core Data, I could create a modal, a entity called TodoList and attributes like title, text. First question: Can I create attributes for videos and pictures as well and store them here?
Suppose I have 500 different tasks, I will have to create a view controller for each, right? How would I relate these tasks to each user, like the example above? So each user would have his own tableview with the tasks leading to view controllers.
Assuming it is possible to make this work, how could I change the tasks assigned to each user directly, without having to use xcode or doing it programatically?
Use Core Data to store all your required fields in which image and video can also reside in Core Data. Use predicate to fetch the data according to constraints imposed on each user. When he will open the app, app should be able to fetch data according to user condition. After this you can load your table.
Hope this helps.
If we consider TO-DO apps , if a user should be able to see his own content , he will have to enter his own content through the app.This data is then stored in some backend either Core Data , SqLite or even cloud backend like Parse.com.Then the user is able to fetch his data , and see the contents in any view ...like tableView in your case.

Resources