user6485277
user6485277

Reputation:

Core Data EXC_BAD_ACCESS

I've got my own Core Data function, which fetches data. This func is identical to my previous one, except entity names. Previous works great, but this fails with error EXC_BAD_ACCESS when I try to get data from fetchedData .

func fetchGroups() -> Array<Group> {
    var groups: Array<Group> = []
    let fetchRequest: NSFetchRequest<Public> = Public.fetchRequest()
    fetchRequest.sortDescriptors = [SortDescriptor.init(key: "publicTitle", ascending: true)]
    let fetchedData = try! context.fetch(fetchRequest)
    if (!fetchedData.isEmpty) {
        for i in 0...fetchedData.count-1 {
            print(fetchedData[0])
            var group: Group = Group()
            group.groupName = fetchedData[i].publicTitle
            group.groupPhoto = fetchedData[i].publicPhoto
            group.groupID = Int(fetchedData[i].publicID)
            groups.append(group)
        }
        return groups
    }
    else {
        return groups
    }
}

So if it executes the code in brackets after if (!fetchedData.isEmpty), array isn't empty. Why it fails on getting elements?

P.S. fetchedData.count = 1; But fetchedData[0] = BIG CRASH! Magic.

Upvotes: 0

Views: 479

Answers (1)

user6485277
user6485277

Reputation:

After a long research I found that the problem was too little but very hard to find. I just checked my coredata.xcdatamodeld file and noticed, that Public entity doesn't have Class definition. By default all entities have only names.

Upvotes: 3

Related Questions