In CollectionView I am displaying datas from parse.com. Successfully retrieved. But unable to display in the cell. I am receiving error as Array outbound. I found out the mistake, parse is running as asynchronous. But, before parse end, collection view gets loaded. So I unable to display values in the cell. It is throwing an error. How to stop all the process until parse get loaded completely? Kindly guide me. MY CODING IS BELOW:
//VIEWCONTROLLER having collectionView
var myList : NSArray = NSArray()
let obj_par = parse_retrive()
obj_par.parse_DB() //DATAS GOING TO RETRIVE FROM PARSE
story_collection_view.reloadData() //RELOADING COLLECTIONVIEW
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
println("fifth")
if(collectionView == date_collection_view)
{
:
:
return cell
}
else
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("story_read", forIndexPath: indexPath) as story_reading_cell
cell.story_title.text = myList.objectAtIndex(indexPath.row) as? String //ERROR: ARRAY OUTBOUND OF INDEX
return cell
}
}
//PARSE_RETRIVE CLASS
func parse_DB() {
println("SECOND 10")
par_query.findObjectsInBackgroundWithBlock({(NSArray objects, NSError error) in
if (error != nil) {
NSLog("error " + error.localizedDescription)
}
else {
println("SECOND 13")
let sql_store_obj1 = sql_to_store() //THIRD CLASS
self.parse_obj_arr = NSArray(array: objects)
var j : Int = self.parse_obj_arr.count
for (var i : Int = 0; i < j; i++) {
self.par_object = self.parse_obj_arr.objectAtIndex(i) as PFObject
self.sto = self.par_object["story_title"] as String //STORY TITLE FROM PARSE
self.sto_con = self.par_object["story_content"] as String //STORY CONTENT FROM PARSE
self.sto_tit.append(self.sto) //STORING IN ARRAY VAR
self.sto_cont.append(self.sto_con) //STORING IN ARRAY VAR
} //FOR ENDING
sql_store_obj1.sto_title_arr = self.sto_tit
sql_store_obj1.sto_content_arr = self.sto_cont
sql_store_obj1.parse_to_sql()
}//ELSE ENDING
}) //PARSE QUERY ENDING
println("SECOND")
}
//THIRD CLASS
func parse_to_sql() {
let appDel : AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context : NSManagedObjectContext = appDel.managedObjectContext!
let ent = NSEntityDescription.entityForName("Sad", inManagedObjectContext: context)
var newStory = story_sad_managed(entity: ent!, insertIntoManagedObjectContext: context)
newStory.story_title = sto_title_arr
newStory.story_content = sto_content_arr
let request = NSFetchRequest(entityName: "STORY")
request.predicate = NSPredicate(format: "story_title = %@", sto_title_arr)
result = context.executeFetchRequest(request, error: nil)!
let sto_page_obj = story_page()
sto_page_obj.myList = result //1st CLASS ARRAY
}