replace usage of #ifdef with forward classes

Also moves weak ref to a managedObjectContext to be a class method of
the API