How to get all Created Class Names in Parse using swift? - ios

Can anyone please tell me a way to Query the list of classes created in the Parse Core. I am using Swift 2.0 and XCode 7.
Basically, I have 4 different classes (example : Class1, Class2, ClassName3, ClassName4) holding the different data. I want to get only the class Names and display it in my app using PFQueryTableViewController.
override func queryForTable() -> PFQuery {
var query : PFQuery = PFQuery(className: "ClassName1")
query.orderByAscending("songName")
return query
}
Above code queries the data from ClassName1, however, I want to display the class names not the data within a particular class.
Thanks
Thanks a lot

It isn't available via the iOS SDK.
You can get it via the REST interface if you use the master key to fetch the app schema: https://api.parse.com/1/schemas
It may be easier for you to just add a Classes class with rows for each of the classes, which you manage manually and can simply query with PFQuery...

Related

Accessing MultiLevel Nested Children in Firebase Database Swift 3

I am using Firebase Realtime Database.
I have a customer id which corresponds to customer table. I need to fetch its respective apartment name. Then search the record having same apartment name in apartment table. Once found, I need to get all serviceType values from its respective segments. Also need to fetch its block from apartment table.
Table structure is as follows:
customer
-L1x2AKUL_KNTKXyza
apartment_name:"ABC Residency"
appartment
-L1Ohec4nW-ya_SkiG49
apartment_name:"ABC Residency"
block: "A Wing"
segments
-L1OhecGtEk_8xdNs67T
serviceType:"Mopping"
-L1OhecGtEk_8xdNs631
serviceType:"Cleaning"
I want to use only one firebase database reference object and achieve this multilevel access.
Any help would be greatly appreciated!
This should help you. I've created a class called CRUD to perform all of my firebase functions.
import Firebase
/**
CRUD Object to maintain Firebase DB
- MUST use crud.creatRef() before any save or read functions!!
*/
class CRUD: NSObject {
var ref: DatabaseReference!
var id: String!
/**Create FirebaseDB reference for use of CRUD Object to interact with DB*/
func createRef() {
id = PageDataSource.sharedInstance.myID!
ref = Database.database().reference(fromURL: "https://yourURL.firebaseio.com/")
}
}
When calling CRUD to save or load data within my app I have to do this:
let crud = CRUD()
crud.createRef()
crud.doSomething()
I've had great success with this. You'll need to design your model and implement the rest of the code accordingly. When you create your save functions they should go inside of the CRUD object.

Create an instance of a class inside its own self

Here's the situation.
I have a swift model class file that contains all the variables for a weather forecast (min temperature, max temperature, humidity, etc).
The class also contains the function that downloads all the data from the API.
My question is, is it possible to create an array of the class inside the class itself, so that I can append a number of objects (of the same class itself) based on the number of days of forecast the API sends back?
If so, can you tell me how it could be achieved?
The other option I have that totally works, is to do the API downloading and parsing outside of the forecast class (in the ViewController) but that would make my ViewController messy.
You shouldn't call the API to get all the weather's datas in your model but in a dedicated class.
Also, you shouldn't parse and store the datas inside your model.
In your model, you only need to have all the attributes of your object and methods.
Maybe you can create an Helper class where you implements all this methods and store the datas.
There's nothing preventing you from referencing a class name within its definition. So you can do something like this:
class Foo {
var boz = [Foo]() // you can append to this array as needed
}

Connecting remote search results with local database using CoreData

Assume we have simple data model with single entity User; simple tableView_friends with fetchedResultsController_friends for show users - friends.
Assume we have search bar for searching all (not only friends) users in service, and for every typed in it character we perform search request to server, which return to us somehow filtered by character User objects. Some of this objects can already be inside local database. By app logic we don't really must save all this results in local database forever (but ok, we can, we can clear local database time to time); on other hand, if we will perform any action on some searched user, we must store this user. We want to show list of searched user in other tableView_search with fetchedResultsController_search.
Question: should I use same context for fetchedResultsController_friends and fetchedResultsController_search? If no, how can I handle situation, when I wish to edit searched user, which already exists in database and probably already local edited? If yes, how can I setup predicate for fetchedResultsController_search (server perform its own logic for search by character, which can be changed) for show exactly same result as from server?
We recently implemented a search feature in our application and had a similar issue, We had local data in core data and also remote data from our API.
You have a few options that we explored:
Save your data into core data from the API as it is retreived and
then the fetched results controller will do the rest
Manage the merge of the data yourself, you can still use NSFetchedResults controller to an extent but need to do more work
We didn't want to save all of the information returned from the API unless it was needed (the user selected it), so we come up with a simple solution that worked for our app. This may not work directly for your app, you may need a completely different solution or change some of the things we done to suit.
Firstly, To explain what we are dealing with, we had a Article entity in core data which contains around 25 properties, the API returns article objects as JSON data with the same data.
What we decided to do was to create a class which represents a simple version of an article (just enough data to show in a list view and reference it later in the API or core data) which looked something like this:
class SearchResult: NSObject {
var id:String?
var title:String?
var imageUrl:String?
var url:String?
// core data entity
init(article:Article) {
self.id = content.contentId
self.title = content.title
self.featuredImageURL = content.absoluteImagePath()
self.urlAlias = content.urlAlias
self.publishedAt = content.publishedAt
}
init(articleDictionary:NSDictionary) {
self.id = articleDictionary.objectForKeyNotNull("id") as? String
self.title = articleDictionary.objectForKeyNotNull("title") as? String
self.url = articleDictionary.objectForKeyNotNull("url") as? String
if let imageUrl = articleDictionary.objectForKeyNotNull("imageUrl") as? String {
self.imageUrl = imageUrl
}
}
}
Now using this, we can create once of these from either the core data results or from the API results. Our tableview datasource is just an array
var dataSet = [SearchResult]()
We use the NSFectchResultsController delegate methods to add/remove/re-order core data elements from the dataSet after the initial load and when we get API data we'll do something like:
dataSet = Array(Set(apiResponseArray + dataSet))
This will take an array of SearchResult items from the API, merge them with the current result set and remove duplicates. casting to a set and then back to an array will give you an array of unique results as a Set is made of unique values only.
See this reference which should help with how the delegate methods would work

Querying multiple (n=7) classes with pointers

I need to produce a 'feed' of activities using a PFQueryTableViewController.
I have 7 classes that all contain various activities (ie. Paymenmt, Meal, Note....). They all contain pointers to the Child class.
What I'd like to do is query all these classes to pull the latest activities for the corresponding child and return the objects for each class.
I have tried,
override func queryForTable() -> PFQuery {
let queryNote = PFQuery(className: "Note")
queryNote.whereKey("child", equalTo: passedChildID!)
let queryPayment = PFQuery(className: "Payment")
queryItem.whereKey("child", equalTo: passedChildID!)
return queryNote
}
However I am running into trouble as I can't return an array of queries.
I also tried query.orQueryWithSubQueries but that only works with queries of the same class.
Parse documentation gives examples, but only for 2 classes (for example).
query.whereKey("post", equalTo: PFObject(withoutDataWithClassName: "Post", objectId: "1zEcyElZ80"))
Any help would be appreciated.
Unfortunately, there is no way to do this. You can only query 1 class at a time.
A possible workaround could be to create an Activity class that contains the gist of each activity and then add a pointer to the correct activity (i.e. a Meal).
In this way, you could query for all the latest activities, and also get the Meal/Payment/Note objects using the include statement.
However, each Activity object would need to have a column for each type of activity, so if you had 10 different activity classes like Meal, the Activity class would then need 10 such pointer columns. It is not a beautiful workaround, but it would work.
Not knowing your datamodel I would still advise that you rethink it and see if it could be possible to do it differently. When you develop a mobile application you should plan your data model based on the queries you need to make rather than the old-fashioned RDBM way of thinking strictly in autonomous objects.

how to get the list of keys in a Parse.com query?

Is there any option to get only the list of existing keys instead of getting all of them in a query?
i'm using PFQueryTableViewController to build my Table View
i have a Parse.com class that contains list of keys which can duplicate themselves
need to build table view that show only the list of unique keys from that class
can i do that using parse querying methods while overriding func queryForTable() -> PFQuery
Using swift for ios
Here you can find documentation: Basic Queries

Resources