Monotouch - Error setting AVAssetWriterInput - unrecognized selector sent to instance - xamarin.ios

When I try to set the audio configuration writer input I'm getting an error:
-[__NSCFNumber length]: unrecognized selector sent to instance 0xbcdab0
My code:
NSObject[] values = new NSObject[]
{
NSNumber.FromFloat(44100.0f),
NSNumber.FromInt32((int)MonoTouch.AudioToolbox.AudioFormatType.MPEG4AAC),
NSNumber.FromInt32(2),
NSNumber.FromInt32((int)AVAudioQuality.Medium)
};
//Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary
NSObject[] keys = new NSObject[]
{
AVAudioSettings.AVSampleRateKey,
AVAudioSettings.AVFormatIDKey,
AVAudioSettings.AVNumberOfChannelsKey,
AVAudioSettings.AVChannelLayoutKey
};
//Set Settings with the Values and Keys to create the NSDictionary
NSDictionary audiosettings = NSDictionary.FromObjectsAndKeys (values, keys);
writeraudioInput = new AVAssetWriterInput (AVMediaType.Audio, audiosettings);

Seems like you're setting key to AVChannelLayoutKey and value to type of AVAudioQuality. They're no corresponding to each other. AVChannelLayoutKey's value should contant struct of AudioChannelLayout.
Comment both last key and last values. Or fill and pass AudioChannelLayout structure.

You are using the loosely typed NSDictionary-based API.
You could avoid these errors in the future if you use the other constructor that takes an AudioSettings parameter, which conveniently provides intellisense for you.

Related

Can't pass Core Data object between Swift classes

I have an array of Core Data entities that I can successfully pass from Objective-C VC to a Swift VC. I've verified through the console that they are coming in.
Now I need to loop through the array of core data objects and pass them onto another swift vc. I have the array set up as a property (Customer is the CD entity):
var arrCustomers: Array = Customer
In my loop I am trying to pass them to the other Swift VC:
for thisCustomer in self.arrCustomers {
let gridCell = storyboard.instantiateViewController(withIdentifier: "bulkAddCell") as! BulkAddCell
resizeGridCell(vGridCell: gridCell.view, frameY: frameY, frameH:50.0)
gridCell.thisCustomer = thisCustomer
self.svData.addSubview(gridCell.view)
}
In BulkAddCell I have thisCustomer as a property:
var thisCustomer: Customer!
I know BulkAddCell is initializing correctly as I have some IBOutlets in it and I can see them in the console. But thisCustomer is always nil.
I'm making the switch from Obj-C to Swift so I am sure it is something basic I am missing.
Change:
var arrCustomers: Array = Customer
To:
var arrCustomers = [Customer]() //init to an empty array of customer objects
//sometime later
//append your arrCustomers with instances of Customer
// before leaving first view controller access the arrCustomers object
// contents as needed and assign to a corresponding property on the
// destination view controller. (usually in preparforSegue)

Field index for queries not updating when value set programmatically

My module creates a custom content item through the controller:
private ContentItem createContentItem()
{
// Add the field
_contentDefinitionManager.AlterPartDefinition(
"TestType",
cfg => cfg
.WithField(
"NewField",
f => f
.OfType(typeof(BooleanField).Name)
.WithDisplayName("New Field"))
);
// Not sure if this is needed
_contentDefinitionManager.AlterTypeDefinition(
"TestType",
cfg => cfg
.WithPart("TestType")
);
// Create new TestType item
var newItem = _contentManager.New("TestType");
_contentManager.Create(TestItem, VersionOptions.Published);
// Set the added boolean field to true
BooleanField newField = ((dynamic)newItem).TestType.NewField as BooleanField;
newField.Value = true;
// Set title (as date created, for convenience)
var time = DateTime.Now.ToString("MM-dd-yyyy h:mm:ss tt", CultureInfo.InvariantCulture).Replace(':', '.');
newItem.As<TitlePart>().Title = time;
return newItem;
}
The end result of this is a new TestType item with a field that's set to true. Viewing the content item in the dashboard as well as examining ContentItemVersionRecord in the database confirms that the value was set correctly.
However, queries don't seem to work properly on fields that are set in this manner. I found the record IntegerFieldIndexRecord, which is what I assume projections use to fill query result pages. On this, the value of TestField remains at 0 (false), instead of 1 (true).
Going to the content item edit page and simply clicking 'save' updates IntegerFieldIndexRecord correctly, meaning that the value is now picked up by the query. How can the record be updated for field values set programmatically?
Relevant section of migration:
SchemaBuilder.CreateTable(typeof(TestTypePartRecord).Name, table => table
.ContentPartRecord()
);
ContentDefinitionManager.AlterTypeDefinition(
"TestType",
cfg => cfg
.DisplayedAs("Test Type")
.WithPart(typeof(TitlePart).Name)
.WithPart(typeof(ContainablePart).Name)
.WithPart(typeof(CommonPart).Name)
.WithPart(typeof(IdentityPart).Name)
);
Edit: The fix for this is to manually change the projection index record whenever changing a field value, using this call:
_fieldIndexService.Set(testResultItem.As<FieldIndexPart>(),
"TestType", // Resolves as TestTypePart, which holds the field
"newField",
"", // Not sure why value name should be empty, but whatever
true, // The value to be set goes here
typeof(bool));
In some cases a simple contentManager.Publish() won't do.
I've had a similar problem some time ago and actually implemented a simple helper service to tackle this problem; here's an excerpt:
public T GetStringFieldValues<T>(ContentPart contentPart, string fieldName)
{
var fieldIndexPart = contentPart.ContentItem.As<FieldIndexPart>();
var partName = contentPart.PartDefinition.Name;
return this.fieldIndexService.Get<T>(fieldIndexPart, partName, fieldName, string.Empty);
}
private void SetStringFieldValue(ContentPart contentPart, string fieldName, IEnumerable<int> ids)
{
var fieldIndexPart = contentPart.ContentItem.As<FieldIndexPart>();
var partName = contentPart.PartDefinition.Name;
var encodedValues = "{" + string.Join("},{", ids) + "}";
this.fieldIndexService.Set(fieldIndexPart, partName, fieldName, string.Empty, encodedValues, typeof(string));
}
I've actually built this for use with MediaLibrary- and ContentPicker fields (they encode their value as string internally), so it might not be suitable for the boolean field in your example.
But it can't be that hard to implement, just look at the existing drivers and handlers for those fields.
There are 2 ways to fix this:
1) Ensure the newly created item is getting published by calling ContentManager.Publish() as Orchard.Projections.Handlers.FieldIndexPartHandler listens to the publish event to update the FieldIndexPartRecord
2) use IFieldIndexService to update FieldIndexPartRecord manually, see implementation of Orchard.Projections.Handlers.FieldIndexPartHandler to get in idea how to do this
Hope this helps.
:edit
Due to calling Create(...Published) the ContentManager.Published() won't do anything as the item is already considered published.
You can do the following to force the publish logic to run:
bool itemPublished = newItem.VersionRecord.Published;
// unpublish item first when it is already published as ContentManager.Publish() internally first checks for published flag and when set it aborts silently
// -> this behaviour prevents calling publish listeners
if (itemPublished)
_contentManager.Unpublish(newItem);
// the following call will result in calls to IContentHandler.Publishing() / IContentHandler.Published()
_contentManager.Publish(newItem);
or just create the item as a draft and publish it when everything is setup correctly.

Swift CoreData subclass not working

i know that this has been asked a couple of times but the usual solution does not seem to work for me. I created a CoreData entity and a subclass for it using <ProjectName>.<SubclassName> syntax as class name. Creating a new object like this:
let object = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: CoreDataManager.sharedInstance.managedObjectContext) as User
println("-->\(object)<--")
object.setValue(12, forKey: "userID")
object.setValue("username", forKey: "username")
the console output:
although the object does not get printed in the console it seems to have been in some way created and setting a value on that object refers to a Core Data Object
when i use it without a subclass it works as expected (setting Class name back to default):
let object = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: CoreDataManager.sharedInstance.managedObjectContext) as NSManagedObject
println("-->\(object)<--")
output:
Here's my subclass declaration:
import Foundation
import CoreData
class User: NSManagedObject {
#NSManaged var userID: NSNumber
#NSManaged var username: String
}
and here's the core data model form:
What's wrong with the code? Or do i miss anything?
For printing out the value of an NSManagedObject, do not use:
println(...)
But rather use:
NSLog(...)
Please find an example of using NSLog and an NSManagedObject below:
/**
* Called when the user clicks on the save button.
*/
#IBAction func saveTapped(sender: AnyObject) {
// Reference to our app delegate
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
// Reference MOC
let context: NSManagedObjectContext = appDel.managedObjectContext!
let en = NSEntityDescription.entityForName("List", inManagedObjectContext: context)
// Create instance of put data model and initialize
var newItem: List = List(entity: en!, insertIntoManagedObjectContext: context)
// Map our properties
newItem.item = textFieldItemName.text
newItem.quantity = textFieldQt.text
newItem.info = textFieldMoreInfo.text
// Save our context
var error: NSError? = nil;
if (context.hasChanges) {
if (!context.save(&error)) { // save failed
println("Save Failed: \(error!.localizedDescription)")
} else {
println("Save Succeeded")
}
}
NSLog("newItem: %#", newItem)
// Navigate back to root ViewController
self.navigationController?.popToRootViewControllerAnimated(true)
}
Note: I do not know the exact reason (bug, or implementation maybe ...) but it turns out that it does not print out a value when we use println(...) function, instead of that it returns an empty String.
Consequently I recommend to all of you guys to use NSLog(...) function instead of println(...) when you want to print out the value of an NSManagedObject.
If you print the expression CoreDataManager.sharedInstance.managedObjectContext twice, do you get a different pointer each time?
It sounds like the managed object context is getting deallocated right after you use it, which indicates that your CoreDataManager.sharedInstance.managedObjectContext property is returning a new managed object context every time, not the same one, or your sharedInstance property is returning a new instance every time.
A managed object's in-memory state is stored as a weak reference to a managed object context. When the context drops out from underneath you (e.g. it's no longer referenced and so is deallocated), your managed object's storage disappears.
A few good indicators that this is happening:
You haven't saved your managed object context yet but a newly created managed object prints as fault
You get weird errors when you try to set a property
You get weird errors when you try to retrieve a property you just set
Include the following at the top of your .swift file
import CoreData
The reason for not working is it doesn't know which library to reference.
Hope it helps
try creating it with this method :
let entityDescripition = NSEntityDescription.entityForName(“User”, inManagedObjectContext: managedObjectContext)
let user = User(entity: entityDescripition, insertIntoManagedObjectContext: managedObjectContext)

Error during run OrganizationRequest 'RetrieveAllEntities'

I got this error during execute, could anyone give suggestion? Thanks
OrganizationRequest oreq = new OrganizationRequest();
oreq.RequestName = "RetrieveAllEntities";// please google for available Request Names
oreq.Parameters = new ParameterCollection();
oreq.Parameters.Add(new KeyValuePair<string, object>("EntityFilters", EntityFilters.Entity));
oreq.Parameters.Add(new KeyValuePair<string, object>("RetrieveAsIfPublished", false));
OrganizationResponse respo = orgProxy.Execute(oreq);
"The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter schemas.microsoft.com/xrm/2011/Contracts/Services:ExecuteResult. The InnerException message was 'Error in line 1 position 727. Element 'schemas.datacontract.org/2004/07/System.Collections.Generic:value' contains data of the 'schemas.microsoft.com/xrm/2011/Metadata:ArrayOfEntityMetadata' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'ArrayOfEntityMetadata' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details."
Add a reference to Microsoft.Crm.Sdk.Proxy and Microsoft.Xrm.Sdk. Visual Studio may tell you that you need to add an additional couple System.* references - add them.
Use this code:
IOrganizationService service = GetCrmService(connectionString); //This is a helper, just need to setup the service
var request = new Microsoft.Xrm.Sdk.Messages.RetrieveAllEntitiesRequest()
{
EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.All,
RetrieveAsIfPublished = false
};
var response = (Microsoft.Xrm.Sdk.Messages.RetrieveAllEntitiesResponse)service.Execute(request);
Get it work finally there is two KnownTypeAttribute need to be added to the proxy class
**[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityMetadata[]))]**
public partial class OrganizationRequest : object, System.Runtime.Serialization.IExtensibleDataObject
....
**[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityMetadata[]))]**
public partial class OrganizationResponse : object, System.Runtime.Serialization.IExtensibleDataObject
Thank you for help.

How to hide code from editor using a projection

I am working on a custom language service. Code files of the language contain both metadata and source code, whereby metadata is stored as a header (at the beginning of the file; this requirement can´t be changed). The language service provides two logical views; one for the code and another one allowing to edit the metadata.
Now, the problem is that I want to hide the metadata from the code editor view - and I thought implementing it using projection is the way to go, but the Visual Studio SDK does neither contain any examples nor a good explanation on how to implement such functionality. Just a short explanation about the concept is given at: Inside the Editor, Projection
I also found a language service project at Codeplex: Python Tools for Visual Studio, which has Django support (in this project projection buffers are used to mix HTML markup and Python code), but this seems to be a different scenario...
This is what I tried...
Within my editor factory, I create an IVsTextLines instance that is used by the code editor view using the following code...
private IVsTextLines GetTextBuffer(IntPtr docDataExisting)
{
if (docDataExisting == IntPtr.Zero)
{
Type t = typeof(IVsTextLines);
Guid clsId = typeof(VsTextBufferClass).GUID;
Guid interfaceId = t.GUID;
IVsTextLines bufferInstance;
if ((bufferInstance = this.Package.CreateInstance(ref clsId, ref interfaceId, t) as IVsTextLines) != null)
{
object oleServiceProvider = this.GetService(typeof(IServiceProvider));
var withSite = (IObjectWithSite)bufferInstance;
withSite.SetSite(oleServiceProvider);
return bufferInstance;
}
}
return null;
}
From what I´ve seen so far is that the IVsTextLines interface is not compatible with IProjectionBuffer, ITextBuffer and so on. I learned from the Python Tools project that I can use a IVsEditorAdaptersFactoryService to get an ITextBuffer instance from the IVsTextLines object; the adapter factory service (as well as some other required service instances) gets injected into my editor factory via MEF...
IVsTextBuffer textBuffer = this.GetTextBuffer(...);
ITextBuffer documentBuffer = this.editorAdaptersFactoryService.GetDocumentBuffer(textBuffer);
The ITextBuffer can be used as a source buffer for projections... my idea is to create an elision buffer having to spans; one for the metadata and one for the code, whereby the metadata span will be elided which makes the code disapear, and use this elision buffer as the projection. This is what I tried...
private ITextBuffer CreateBuffer(
IProjectionEditResolver editResolver,
ITextBuffer documentBuffer)
{
IContentType contentType = this.ContentTypeRegistryService.GetContentType("...");
ITextSnapshot currentSnapshot = documentBuffer.CurrentSnapshot;
string text = currentSnapshot.GetText(0, currentSnapshot.Length);
TextRange textRange = Parser.GetMetadataTextRange(text); // parse code; return a text range representing the code to elide
ITrackingSpan elideSpan = currentSnapshot.CreateTrackingSpan(textRange.Start, textRange.Length, SpanTrackingMode.EdgeExclusive);
int offset = textRange.Start + textRange.Length;
ITrackingSpan expandSpan = currentSnapshot.CreateTrackingSpan(offset, currentSnapshot.Length - offset, SpanTrackingMode.EdgeInclusive);
SnapshotSpan elide = elideSpan.GetSpan(currentSnapshot);
SnapshotSpan expand = expandSpan.GetSpan(currentSnapshot);
var snapshotSpans = new List<SnapshotSpan>
{
elide,
expand
};
var spanCollection = new NormalizedSnapshotSpanCollection(snapshotSpans);
const ElisionBufferOptions BufferOptions = ElisionBufferOptions.FillInMappingMode;
IElisionBuffer buffer = this.ProjectionBufferFactoryService.CreateElisionBuffer(editResolver, spanCollection, BufferOptions, contentType);
buffer.ModifySpans(new NormalizedSpanCollection(elide), new NormalizedSpanCollection(expand));
return buffer;
}
Once the elision buffer is created, I use the SetDataBuffer method to connect it with the editor...
ITextBuffer buffer = this.CreateBuffer(..., documentBuffer);
this.editorAdaptersFactory.SetDataBuffer(textBuffer, buffer);
This works somehome; at least the metadata code disapears from the code editor, but as soon as try to edit the code, I got an InvalidOperationException, which is not the case, if I create the elision buffer with just a single span, or if I don´t hide the elideSpan.
The following error is reported to the ActivityLog:
System.InvalidOperationException: Shim buffer length mismatch. Document=11594 Surface=11577 Difference at 0
at
Microsoft.VisualStudio.Editor.Implementation.VsTextBufferAdapter.OnTextBufferChanged(Object sender, TextContentChangedEventArgs e)
at
Microsoft.VisualStudio.Text.Utilities.GuardedOperations.RaiseEvent[TArgs](Object sender, EventHandler`1 eventHandlers, TArgs args)
I think I am quite close to the final solution; so what I am doing wrong?

Resources